C Preprocessors

 Preprocessors: are programs that process the source code before compilation.

Commands of preprocessors begins with # and compiler does not understands # statements.
These are not the part of compiler but the step in compilation.

Preprocessors Directives  

A preprocessor is  a program that process the source code before it passes through the compiler. All these preprocessor directive begins with # symbol. The # symbol indicates that whatever statements starts with a # will go to the preprocessor program to get executed.

Types of preprocessors:
1. Macros
2. File inclusion
3. Conditional compilation
4. Other directives

Macros

Macros is a piece of code in a program that is given some name. Whenever this name is encountered by compiler, the compiler replaces the name with actual piece of code. Macros is defined by #define directive.

There are two types of macros:
1.    Object-like Macros
2.    Function-like Macros

Predefined preprocessors

__DATE__        -> represents current date in "MMM DD YYYY" format
__TIME__        ->represents current time in "HH:MM:SS" format
__FILE__          ->represents current file name
__LINE__         ->represents current line number
__STDC__       ->It is defined as 1 when compiler complies with the ANSI standard.

Advantages of using Macros

  • Code reuse: By allowing developers to declare a piece of code just once and use it several times.
  • Code abbreviation: Macros make it possible to write clear, expressive code that is simpler to read and comprehend the intentions of the programmer.
  • Performance Optimization: By minimizing function call overhead, macros may be utilized to optimize code execution


File inclusion

This type of preprocessor tells the compiler to include a file in source program.
The #include preprocessor directive is used to include a file in source program .

Two types of file can be included by user in program:
1. Standard header file
2. User defined header file
 

By the use of #include directive, we provide information to the preprocessor where to look for the header files. There are two variants to use #include directive.

1.    #include <filename> //path
2.    #include "filename" // directory

The #include <filename> tells the compiler to look for the directory where system header files are held. In UNIX, it is \usr\include directory.

The #include "filename" tells the compiler to look in the current directory from where program is running.

Examples:

1.    #include<stdio.h>  

2.     int main(){    

3.       printf("Hello C");      

4.       return 0;  

5.     }  


Conditional compilation

Using preprocessor directives like #if, #ifdef, #ifndef, #else, and #endif
you can include or exclude parts of the code during compilation based on defined conditions.

#endif directive is used to close off the #if, #ifdef, and #ifndef opening directives which means the preprocessing of these directives is completed.

Example:

#include<stdio.h>
#define A 50
#define B 7
int main()
{
        int result;
        #if(A>B)
        result=A+B;
        printf("sum=%d",result);
        #else
        result=A-B;
        printf("diff=%d",result);
        #endif
        return 0;
}


Other Directives

There are 2 more directive that are not commonly used. They are :
1. #undef directive
2. #pragma directive

#undef directive:

#undef is used to undefine an existing macro.
syntax: #undef TOKEN
Example:

#include <stdio.h>
#define VALUE 10
int main() {
printf("Min value is: %d\n", VALUE);
#undef VALUE
#define VALUE 20
printf("Min value after undef and again redefining it: %d\n", VALUE);
return 0;
}


#pragma directive

The #pragma preprocessor directive is used to provide additional information to the compiler. The #pragma directive is used by the compiler to offer machine or operating-system feature. Also used to hide the information.

Pragma tokens

Different compilers can provide different usage of #pragma directive.
The turbo C++ compiler supports following #pragma directives.


1.    #pragma argsused  
2.    #pragma exit  
3.    #pragma hdrfile  
4.    #pragma hdrstop  
5.    #pragma inline  
6.    #pragma option  
7.    #pragma saveregs  
8.    #pragma startup  
9.    #pragma warn  \\ hide warning in c programing


Questions:

Which preprocessor directive is used to terminate conditional compilation? 
#endif

Which preprocessor directive is used to declare macros?
#define

List any 4 preprocessing directives? 
#include
#define
#pragma
#if

What are preprocessor directives? 
A preprocessor is  a program that process the source code before it passes through the compiler.

What is the use of #include preprocessor directive? 
 The #include preprocessor directive is used to include a file in source program .


Popular Posts