Check The Programming Section
Preprocessor directives are typically used to make the code easy to modify and compile in different execution environments. Directives in the source file will tell the compiler to take specific action. Preprocessor lines are identified and executed before the macro expansion. Preprocessor statements use the same character set used by the source file. Only change is that escape characters are not supported by the preprocessor.
In preprocessor statement, the number sign (#) must be the first character on the line containing a directive. White-space can appear in between the number sign and the first character of the directive as follows:
# include<stdio.h>
int main(){
int a = 4, b = 5;
printf("%d %d",a,b);
return 0;
}
In the above example, there is a white-space in between the number sign (#) and the first character. Preprocessor directives can be placed anywhere in the code. But only useful after that only. Look at the example given below:
#include<stdio.h>
#define print printf("%d %d",a,b)
int main(){
int a = 4, b = 5;
print;
#define print(a,b) printf("%d %d",a,b)
print(a,b);
return 0;
}
In the above example, print is defined two times. At first, before the main method, without any parameter. Thereafter, print is redefined inside the main with parameters. After the redefinition, the first version of the print macro can’t be used anymore in the code. Wherever we declare a preprocessor it becomes global. Consider the example given below:
#include<stdio.h>
#define print printf("%d %d",a,b)
void fun(int, int);
int main(){
int a = 4, b = 5;
print;
#define print(a,b) printf("%d %d",a,b)
print(a,b);
fun(a,b);
return 0;
}
void fun(int a, int b){
print(a,b);
}
In the above example, the preprocessor declared inside the main, is also used inside the fun method. If we try to use the first version of the preprocessor inside the fun method, it raises an error as print is not declared. Consider the example given below:
#include<stdio.h>
#define print printf("%d %d",a,b)
void fun(int, int);
int main(){
int a = 4, b = 5;
print;
#define print(a,b) printf("%d %d",a,b)
print(a,b);
fun(a,b);
return 0;
}
void fun(int a, int b){
print; //raised an error
}
Any text follows a directive, except an argument or value need to be preceded by the single-line comment delimiter (//) or enclosed by the comment delimiter (/**/). A preprocessor directives can be continued into multiple lines using an end-of-line marker backlash (\). Consider the example given below:
#include<stdio.h>
#define /*any text*/ set a\
=10
int main(){
int a;
set;
printf("%d",a);
return 0;
}
In C Programming preprocessor recognizes the following directives: