Work of C preprocessor is to change the file related to source coding. It is also to make inlined code with the help of macros. It also puts off the compilation of code more than one time.
Preprocessor has 3 ways of utilization which are directives, constants, and macros. The first one Directive has commands. These commands inform to preprocessor not to use that section of file. The beginning of directives has # sign.
Header Files
For getting text and placing in the present file, # directive is used. It is written in the start of program.
Constants
We can say:
The “identifier name” is always substituted by value. The arithmetical expression should be in parenthesis like that:
In this way, your constants will not be damaged.
If there is no parenthesis, then:
The answer will multiply 1 with four, which is wrong
It is best for conjunction among the other pack of directives.
Provisional assemblage
There are selections to describe if the preprocessor can erase the code previous to the compilation. It has #else, #if, #ifndef and many more but al of them have #endif at the end.
Remarking Code
Provisional assemblage is helpful to remark a set of code.
#endif
Keep away from Counting Files Manifold Times
One major trouble is that header file has used in many other files and due to that it get described many times. Here, preprocessor helps in comprising one file for one time.
“#ifndef” helps to describe the term. It makes sure that code is used one time during the stacking of file.
#define _FILE_NAME_H_
#endif // #ifndef _FILE_NAME_H_
You can only write (#define _FILE_NAME_H_) for defining the expression.
Parallel way is used for others for example Null.
#define NULL (void *)0
#endif // #ifndef NULL
The good thing is to make remark that what is the statement which is terminated by #endif.
Macros
Preprocessor has a function of describing macro. One of the benefits of macro is; it does not let any function to be called again. Macros can be described in this way:
Plain addition macro has this format:
This is not a simple task. It has number of difficult points to understand. If you think to write:
After that,
So, what will be the result?
The result will not be according to your requirement. So, put the parenthesis:
So, the parenthesis will prevent you from getting any wrong value.
int y = ADD_FIVE(3) * 3;
Here, you have to put brackets for getting exact value.
int y = ADD_FIVE(3) * 3;
Brackets will save you from getting any error.
int y = 10;
int z = 5;
SWAP(y, z);
if(y < 0)
SWAP(y, z);
The main aim is to show that all statements must have parenthesis.
What happen when code will be like as:
int x = 11;
int y = 6;
int z = 3;
if(x < 0)
SWAP(x, y);
else
SWAP(x, z);
The program will not run due to semicolon.
int x = 11;
int y = 6;
int z = 3;
if(x < 0)
SWAP(x, y);
else
SWAP(x, z);
Now, the program will run properly.
More Gotchas
So, you have got an idea, why macros are difficult in use? The way of increasing value is also a bad point. They do not change the value. They only print it.
int a = 5, b = 10;
int c = MAX(a++, b++);
The result will be:
The problem here is that y++ ends up being evaluated twice! The nasty consequence is that after this expression, y will have a value of 12 rather than the expected 11. This can be a real pain to debug!
Various line macros
Now, we will use many lines macro.
For example:
x ^= y; \
y ^= x; \
x ^= y; \
}
It is not necessary to put slash in the end. Slash shows that macro is go on the new line.
Highly developed Macro actions
A macro can do a lot of functions on arguments. It can change them in strings. It can stick them collectively.
Publishing Tokens
The argument, which goes to the macro is called token. From time to time, arguments get published jointly and make a token. It will create trouble. The good thing is that to put a macro in the structure for printing.
Double # will use for pasting more than one argument.
For example:
It will come as:
String-izing Tokens
It is good to twist the token in the string. The format is:
Developed form:
It can be used for printing or even get printed itself.