Unit-4: Introduction to C preprocessor

what is c preprocessor?

The C Preprocessor is not a part of the compiler, but is a separate step in the compilation process. In simple terms, a C Preprocessor is just a text substitution tool and it instructs the compiler to do required pre-processing before the actual compilation.

The C preprocessor is a macro processor that is used automatically by the C compiler to transform your program before actual compilation. It is called a macro processor because it allows you to define macros, which are brief abbreviations for longer constructs.

The C preprocessor provides four separate facilities that you can use as you see fit:

  • Inclusion of header files. These are files of declarations that can be substituted into your program.
  • Macro expansion. You can define macros, which are abbreviations for arbitrary fragments of C code, and then the C preprocessor will replace the macros with their definitions throughout the program.
  • Conditional compilation. Using special preprocessing directives, you can include or exclude parts of the program according to various conditions.
  • Line control. If you use a program to combine or rearrange source files into an intermediate file which is then compiled, you can use line control to inform the compiler of where each source line originally came from.

how c preprocessor works

Related image

preprocessor directives

All preprocessor commands begin with a hash symbol (#). It must be the first nonblank character, and for readability, a preprocessor directive should begin in the first column. The following section lists down all the important preprocessor directives −

Image result for c preprocessor directives

Macro sustitution directives

Macro substitution

Macro substitution has a name and replacement text, defined with #define directive. The preprocessor simply replaces the name of macro with replacement text from the place where the macro is defined in the source code.

Macro substitution

Now we will see the fact through an example.

  1. #include <stdio.h>
  2. #define PI 3.1415
  3. int main()
  4. {
  5. float radius, area;
  6. printf("Enter the radius: ");
  7. scanf("%f", &radius);
  8. // Notice, the use of PI
  9. area = PI*radius*radius;
  10. printf("Area=%.2f",area);
  11. return 0;
  12. }

Example 2: Using #define preprocessor

  1. #include <stdio.h>
  2. #define PI 3.1415
  3. #define circleArea(r) (PI*r*r)
  4. int main() {
  5. float radius, area;
  6. printf("Enter the radius: ");
  7. scanf("%f", &radius);
  8. area = circleArea(radius);
  9. printf("Area = %.2f", area);
  10. return 0;
  11. }

file inclusion directives

  1. File inclusive Directories are used to include user define header file inside C Program.
  2. File inclusive directory checks included header file inside same directory (if path is not mentioned).
  3. File inclusive directives begins with #include
  4. If Path is mentioned then it will include that header file into current scope.
  5. Instead of using triangular brackets we use “Double Quote” for inclusion of user defined header file.
  6. It instructs the compiler to include all specified files.

Ways of including header file

way 1 : Including Standard Header Files

  • Search File in Standard Library

way 2 :User Defined Header Files are written in this way

  • Search File in Current Library
  • If not found , Search File in Standard Library
  • User defined header files are written in this format

Live Example :

#include<stdio.h>     // Standard Header File
#include<conio.h>     // Standard Header File
#include"myfunc.h"    // User Defined Header File

Explanation :

  1. In order to include user defined header file inside C Program , we must have to create one user defined header file.
  2. Using double quotes include user defined header file inside Current C Program.
  3. “myfunc.h” is user defined header file .
  4. We can combine all our user defined functions inside header file and can include header file whenever require.

conditional compilation

Conditional Compilation: Conditional Compilation directives help to compile a specific portion of the program or let us skip compilation of some specific part of the program based on some condition.

  1. #ifdef: This directive is the simplest conditional directive. This block is called a conditional group. The controlled text will get included in the preprocessor output iff the macroname is defined. The controlled text inside a conditional will embrace preprocessing directives. They are executed only if the conditional succeeds. You can nest these in multiple layers, but they must be completely nested. In other words, ‘#endif’ always matches the nearest ‘#ifdef’ (or ‘#ifndef’, or ‘#if’). Also, you can’t begin a conditional group in one file and finish it in another.Syntax:
#ifdef MACRO
    controlled text
#endif /* macroname */

2. #ifndef: We know that the in #ifdef directive if the macroname is defined, then the block of statements following the #ifdef directive will execute normally but if it is not defined, the compiler will simply skip this block of statements. The #ifndef directive is simply opposite to that of the #ifdef directive. In case of #ifndef , the block of statements between #ifndef and #endif will be executed only if the macro or the identifier with #ifndef is not defined.
Syntax :

ifndef macro_name

If the macro with name as ‘macroname‘ is not defined using the #define directive then only the block of statements will execute.

3.#if, #else and #elif: These directives works together and control compilation of portions of the program using some conditions. If the condition with the #if directive evaluates to a non zero value, then the group of line immediately after the #if directive will be executed otherwise if the condition with the #elif directive evaluates to a non zero value, then the group of line immediately after the #elif directive will be executed else the lines after #else directive will be executed.

#if macro_condition
#elif macro_condition