Preprocessors in C programming language in C
In this tutorial we are learn about preprocessor its types.
in this tutorial we are learn about following topics
Preprocessors
Micros
Micro with arguments
File inclusion
Nested micros
Conditional compilation
Preprocessors :-
Preprocessor programs provide preprocessors directives which tell the compiler to preprocess the source code before compiling. All of these preprocessor directives begin with a ‘#’ (hash) symbol. The ‘#’ symbol indicates that, whatever statement starts with #, is going to the preprocessor program, and preprocessor program will execute this statement.
There are four main types of preprocessor directives.
Macros
File inclusion
Conditional compilation
Other directors
1). Micros:-
Macros are a piece of code in a program which is given some name. Whenever this name is encountered by the compiler the compiler replaces the name with the actual piece of code. The ‘#define’ directive is used to define a macro.
A macro is a fragment of code that is given a name. You can define a macro in C using the #define preprocessor directive.
There is no semi-colon(‘;’) at the end of macro definition. Macro definitions do not need a semi-colon to end.
Macros are useful for the following purposes:
· To simplify and reduce the amount of repetitive coding
· To reduce errors caused by repetitive coding
· To make an assembly program more readable.
#include<studio.h>
#define count 10
int main ( )
{
for (int i= 0; i<count; i++)
{
printf(" %d \n ", i );
}
return 0;
}
2) File Inclusion:
This type of preprocessor directive tells the compiler to include a file in the source code program. There are two types of files which can be included by the user in the program:
Header File or Standard files: These files contains definition of pre-defined functions like printf(), scanf() etc. These files must be included for working with these functions. Different function are declared in different header files. For example standard I/O functions are in ‘iostream’ file whereas functions which perform string operations are in ‘string’ file.
Syntax:
#include< file_name >
Macros with arguments:
We can also pass arguments to macros. Macros defined with arguments works similarly as functions.
Nesting of micro:-
Listing of micro is a micro may be used in the definition of another micro as illustrated follows:
#define mul(x,y) x * y
#define add(x,y,z) x+y+z
Ex.
#include<studio.h>
#define mul (x,y) x*y
#define add(x,y,z) x+y+z
void main ( )
{
int a = 20; b= 50; c = 2;
Clrscr();
printf("multiplication of two no: ");
printf ("addition is : ");
}
Conditional compilation:-
Conditional compilation are type of directves which helps to compile a specific portion of the program or to skip compilation of some specific part of the code / program based on some conditions.
Conditional compilation as the name implies/ suggest that the condition is compiled if certain condition hold true.
These are done with the help of two processing commands 'ifdef' and 'endif'.
Syntax:
#ifdef macro_name
Statement 1;
...............
...............
Statement N;
#endif