Pre-Processor in C Programming
The C preprocessor is exactly what its name implies. It is a program that processes our source program before it is passed to the compiler.
The preprocessor offers several features called preprocessor directives.
Each of these preprocessor directives begin with a # symbol.
The directives can be placed anywhere in a program but are most often placed at
the beginning of a program, before the first function definition.
We would learn the following preprocessor directives here:
(a) Macro expansion
(b) File inclusion #include<stdio.h> etc..
(c) Conditional Compilation
Macro expansion
Have a look at the following program
#define UPPER 25 int main( ) { int i ; for ( i = 1 ; i <= UPPER ; i++ ); printf ( "\n%d", i ) ; return 0; }
In this program instead of writing 25 in the for loop we are writing it in the form of UPPER, which has already been defined before main( ) through the statement, #define UPPER 25
Macros with Arguments
The macros that we have used so far are called simple macros. Macros can have arguments, just as functions can. Here is an example that illustrates this fact.
#define AREA(x) ( 3.14 * x * x ) int main( ) { float r1 = 6.25, r2 = 2.5, a ; a = AREA ( r1 ) ; printf ( "\nArea of circle = %f", a ) ; a = AREA ( r2 ) ; printf ( "\nArea of circle = %f", a ); return 0; }Output :
Area of circle = 122.656250 Area of circle = 19.625000
In this program wherever the preprocessor finds the phrase AREA(x) it expands it into the statement ( 3.14 * x * x ). However, that’s not all that it does. The x in the macro template AREA(x) is an argument that matches the x in the macro expansion ( 3.14 * x * x ). The statement AREA(r1) in the program causes the variable r1 to be substituted for x.
Thus the statement AREA(r1) is equivalent to: ( 3.14 * r1 * r1 )
Conditional Compilation
We can, if we want, have the compiler skip over part of a source code by inserting the preprocessing commands #ifdef and #endif, which have the general form:
#ifdef macroname statement 1 ; statement 2 ; statement 3 ; #endif
If macroname has been #defined, the block of code will be processed as usual; otherwise not.
void main( ) { #ifdef INTEL code suitable for a Intel PC #else code suitable for a Motorola PC #endif code common to both the computers }
When you compile this program it would compile only the code suitable for a Intel PC and the common code. This is because the macro INTEL has not been defined. Note that the working of #ifdef - #else - #endif is similar to the ordinary if - else control instruction of C.
#if and #elif Directives
The #if directive can be used to test whether an expression evaluates to a nonzero value or not. If the result of the expression is nonzero, then subsequent lines upto a #else, #elif or #endif are compiled, otherwise they are skipped.
A simple example of #if directive is shown below:
main( ) { #if TEST <= 5 statement 1 ; statement 2 ; statement 3 ; #else statement 4 ; statement 5 ; statement 6 ; #endif }
If the expression, TEST <= 5 evaluates to true then statements 1, 2 and 3 are compiled otherwise statements 4, 5 and 6 are compiled.
Training For College Campus
We offers college campus training for all streams like CS, IT, ECE, Mechanical, Civil etc. on different technologies
like
C, C++, Data Structure, Core Java, Advance Java, Struts Framework, Hibernate, Python, Android, Big-Data, Ebedded & Robotics etc.
Please mail your requirement at info@prowessapps.in
Projects For Students
Students can contact us for their projects on different technologies Core Java, Advance Java, Android etc.
Students can mail requirement at info@prowessapps.in