C Preprocessor
C Preprocessor
Ritu Devi
C Preprocessor statements.
• C preprocessor is a program that processes our source
program before it is passed to the compiler
• Features preprocessor directives: The preprocessor offers
several features called preprocessor directives
– Each preprocessor directive starts with a # symbol.
– There can be only one directive on a line.
– There is no semicolon at the end of a directive.
– To continue a directive on next line, we should place a backslash at the
end of the line.
– The preprocessor directives can be placed anywhere in a program
(inside or outside functions) but they are usually written at the
beginning of a program. '
– A directive is active from the point of its appearance till the end of the
program.
• Our program passes through several processors before it is
ready to be executed.
• The main functions performed by the preprocessor directives
are·
1. Macros.
2. File Inclusion.
3. Conditional Compilation.
4. Other directives
Macro Expansion: #define
• The general syntax is-
#define macro_name macro_expansion
• Here macro_name is any valid C identifier, and it is generally
taken in capital letters to distinguish it from other variables.
• The macro_expansion can be any text.
• For example-
• #define PI 3.14159265
• #define MAX 100
• The C preprocessor searches for macro_name in the C source
code and replaces it with the macro_expansion.
• Some more examples:
Macros with Arguments
• The general syntax is
• #define macro_name(arg 1, arg2, ......) macro_expansion
• For example
• some more examples:
• #undef Directive :
The definition of a macro will exist from the #define directive
tiII the end of the program. If we want to undefine this macro
we can use the #undef directive.
– Syntax: #undef macro_name
Problems with Macros
File Inclusion
• This type of preprocessor directive tells the compiler to
include a file in the source code program.
• The filename should be within angular brackets or double
quotes. The syntax is-
#include "filename” /* user defined file*/
#include <filename> /* header file or standard file */
void func1();
void func2();
#pragma startup func1
#pragma exit func2
void func1()
{ printf("Inside func1()\n");
}
void func2()
{
printf("Inside func2()\n");
}
int main()
{
void func1();
void func2();
printf("Inside main()\n");
return 0;
}