Tuesday 30 January 2018

Preprocessors in C

Preprocessors in C

             Preprocessor directives begin with # symbol and they give instruction to the compiler to preprocess a piece of code before actual compilation starts.

File Inclusion

           So far, we have been using preprocessors to include header files for e.g. #include < stdio.h >. A C program may consist of multiple files and we can include the contents of one file into another using the preprocessor directive. Suppose we have a file which contains a function to compute the factorial of a number. We can include this file in any other C file to get the factorial.
Consider the program below :

----------------------------------------------------------------------------------------------------------------------------


 

----------------------------------------------------------------------------------------------------------------------------



           Please note that, no matter how many files are there in a program, main( ) can be present in one file only.
 
           We have used " " instead of < > because factorial.c is not a part of any library. It is present in the current directory. We can use " " for including stdio.h and other header files also. If we use < >, compiler doesn't search for that file in current directory.
 
File inclusion is one use of preprocessors which includes one file into another before compilation starts. Let's explore some other uses of preprocessor directives.

Macros


           We can create symbolic constants called macros using #define preprocessor directive. if we declare a statement like #define PI 3.14, then every occurance of PI in the program is replaced by the value 3.14 before the compilation starts. Macros can also take arguments similar to functions and they can extend over multiple lines also.

Following program illustrates the use of macros :

----------------------------------------------------------------------------------------------------------------------------


 

----------------------------------------------------------------------------------------------------------------------------

Output :
 
Area of circle 1 : 113.040001
Area of circle 2 : 153.860001
Maximum Area is 153

----------------------------------------------------------------------------------------------------------------------------

 The ' \ ' symbol is used to extend macro definition to multiple lines.

           We can see that the macros SQUARE( x ) and MAXIMUM( a, b ) behave like functions. The difference is that MACRO calls are replaced by MACRO definitions by the compiler whereas in case of functions, control is transferred to the function definition whenever a function is called.

Conditional Compilation

            Consider a scenario in which some part of code is architecture specific i.e we need to write different codes for the same function for different architectures. Support we are using INTEL based machine, then we should not compile the code which which is meant for MOTOROLA machines. This kind of conditional compilation is achieved using #define preprocessor directives.

Following example illustrates this :

----------------------------------------------------------------------------------------------------------------------------





----------------------------------------------------------------------------------------------------------------------------
           
            If the statement #define INTEL is present, then the code corresponding to INTEL architecture is compiled and executed. An important use case of conditional compilation is enable or disable debugging statements in a program.

See the program below to understand this feature :

----------------------------------------------------------------------------------------------------------------------------


 

----------------------------------------------------------------------------------------------------------------------------


Output :
 
Current Sum : 1
Current Sum : 3
Current Sum : 6
Current Sum : 10
Current Sum : 15
Current Sum : 21
Current Sum : 28
Current Sum : 36
Current Sum : 45
Current Sum : 55
Sum : 55

----------------------------------------------------------------------------------------------------------------------------

       We see that the macro DEBUG is defined so the debug statement is printed every time. To disable this, just comment out the line where DEBUG is defined. We can completely comment out a piece of code using #if 0 statement. 

----------------------------------------------------------------------------------------------------------------------------



----------------------------------------------------------------------------------------------------------------------------

No comments:

Post a Comment