C Language / Preprocessor Directives

These Statements which are executed before compilation. Preprocessor Directives are statements which start with # symbol. These are used for 3 purpose.They were

1. Macro Variable.
2. Macros Function.
3. Conditional compilation.

Preprocessor Directives Uses Example
Macro defines a constant value
#define pi 3.14
Header file inclusion for a library
#include<filename>
Conditional compilation for including or excluding in source program before compilation with respect to the condition.
set of commands like #ifdef, #endif, #if, #else, #ifndef are used in programs

Process of Preprocessing
Source code -->Preprocessor-->Expanded source code -->compiler Program.

Preprocessor Directives Program
Output
Macro Variable
#include <stdio.h>
#define Pivalue 3.1415
void main()
{
printf("Pi value=%.f",Pivalue);
}
Pi value=3
Macros Function
#include <stdio.h>
#define sq(r) (r*r)
void main() {
printf("Square Value of 3 = %d", sq(3));
}
Square Value of  3 = 9
Conditional compilation
#include<stdio.h>

#define a 20

#define b 10

void main()
{
#if (a>b)
printf("a is bigger than b");

#else
printf("b is bigger than a");

#endif
}
a is bigger than b


Home     Back