C Preprocessor

What are preprocessors in C?
  • Preprocessors are programs that process our source code before compilation.
  • It is also called a macro processor because it allows you to define macros, which are brief abbreviations for longer constructs.

What are preprocessor directives?
  • Preprocessor programs provide preprocessor directives that tell the compiler to preprocess the source code before compiling. 
  • All of these preprocessor directives begin with a ‘#’ (hash) symbol. The statements starting with a ‘#’ symbol will be executed by the preprocessor. 
  • Examples of some preprocessor directives are: #include, #define, #ifndef etc.

Types of Preprocessor Directives
  • File Inclusion
  • Macros
  • Conditional Compilation 
  • Other directives

1. File Inclusion:
The #include directive is a preprocessor directive that is used to include a file in the source program. 

There are two ways of including files using #include :
1.1 Using angular brackets:
If the filename is enclosed within angle brackets, the file is searched for in the standard compiler include paths.


    #include <file_name>


1.2 Using double quotes:
If the filename is enclosed within double quotes, the search path is expanded to include the current source file directory.

    
    #include "file_name"


2. Macros
  • A macro is a piece of code that is mapped to a name or identifier. 
  • Whenever the name is used, it is replaced by the contents of the macro.
  • The #define directive is used to define a macro.

There are two types of macros: 
2.1 Object-like macros : They do not take any parameters.

    
    #define <identifier> <macro-content> 


An example of a object-like macro is:
    
    #define PI 3.14 



2.2 Function-like macros : They take parameters. (although the list of parameters may be empty). 

    
    #define <identifier>(<parameter list>) <macro-content>


An example of a function-like macro is:
    
    #define SUM(X, Y)   (X+Y)


How to remove a macro definition?
A macro definition can be removed with #undef:

Syntax:
    
    #undef <identifier> 


Example:
    
    #undef PI 



Predefined Macros:
The preprocessor defines a number of predefined macros. 
Some of the important predefined macros are 

Predefined MacroDescription
__FILE__It expands to the name of the current input file, in the form of a C string constant.
__LINE__It expands to the current input line number, in the form of a decimal integer constant.
__DATE__It expands to a string constant that describes the date on which the preprocessor is being run. 
__TIME__It expands to a string constant that describes the time at which the preprocessor is being run.
__STDC__It expands to the constant 1, to signify that this compiler conforms to ISO Standard C.
__STDC_VERSION__It expands to the C Standard's version number, a long integer constant of the form yyyymmL where yyyy and mm are the year and month of the Standard version.

Reference: https://gcc.gnu.org/onlinedocs/gcc-3.2.3/cpp/Standard-Predefined-Macros.html
Note:  Each of the above names is prefixed and suffixed by double underscore characters. 
#include <stdio.h>
 int main(){    
   printf("File Name :%s\n", __FILE__ );    
   printf("Line Number :%d\n", __LINE__ );
   printf("Current Date :%s\n", __DATE__ );    
   printf("Current Time :%s\n", __TIME__ );
   printf("STDC :%d\n", __STDC__ ); 
   printf("STDC VERSION :%d\n", __STDC_VERSION__ ); 
   return 0;  
 }  

OUTPUT:
File Name :/tmp/BXFX45KmyX.c
Line Number :5
Current Date :Oct  4 2022
Current Time :13:02:17
STDC :1
STDC VERSION :201710

Post a Comment

0 Comments