C Preprocessors

Updated

Prerequisite – C language
C Preprocessor is not a part of the compiler, but is a separate step in the compilation process. Preprocessors are programs that process our source code before compilation. C Preprocessor is just a text substitution tool that instructs compiler to do required pre-processing before actual compilation.

All preprocessor commands begin with a hash symbol (#). There are 4 main types of preprocessor directives:

  1. Macros
  2. File Inclusion
  3. Conditional Compilation
  4. Other directives

Let us learn in brief.

1. Macros :

The ‘#define’ directive is used to define a macro. Example –

// macro definition 
#define RANGE 10  

Note that there is no semi-colon(‘;’) at the end of macro definition. Macro definitions do not need a semi-colon to end.

We can also pass arguments to macros which works as function:

// macro with parameter 
#define AREA(l, b) (l * b)  

2. File Inclusion :

These tells the compiler to include a file in the source code program.

  • Header File or Standard files –
    These are predefined.

    #include< file_name > 
  • User Defined files –
    We use these for large file.

    #include"filename" 

3. Conditional Compilation :

These help to compile a specific portion of the program or to skip compilation of some specific part of the program based on some conditions. This can be done with the help of two preprocessing commands ‘ifdef‘ and ‘endif‘.

Syntax :

#ifdef macro_name
    statement1;
    statement2;
    statement3;
    .
    .
    .
    statementN;
#endif

4. Other directives :

  • #undef (undefine) Directive –
    #undef RANGE
  • #pragma Directive –
    This directive is a special purpose directive and is used to turn on or off some features.
    #pragma startup and #pragma exit : to specify the functions that are needed to run before program startup( before the control passes to main().
    #pragma warn Directive : to hide the warning message which are displayed during compilation.

Functions of C preprocessor:
The C preprocessor performs the following functions:

1. Includes header files
2. Expands macros
3. Performs conditional compilation
4. Controls lines
5. Inserts files
6. Specifies compile-time error messages
7. Applies machine-specific rules to sections of code
8. Transforms programs before compilation  



Please write comments if you find anything incorrect. A gentle request to share this topic on your social media profile.

Comment
Next tutorial
CPP_Programmer Published 31 Aug 2020 · 2 min read
Topics:

Related tutorials

  • Call by value, call by address and call by reference in C++

    The parameters of the function call are known as actual parameters and the parameters of the function definition are known as formal parameters. There are 3 ways of…

    3 min read
  • What should we use void main() or int main() ?

    There is question that what should we use void main() or int main() ? void main() { /* ... */ } Or, int main() { /* ... */…

    2 min read
  • C Comments

    Prerequisite – C Language Introduction C comments explain code and improve readability. These do not affect program execution. You can use comments in C to clarify code and describe…

    1 min read
  • C Programming Language Standard

    Prerequisite – C Language Introduction The C programming language has various versions: C89/C90, C99, C11, and C18. GATE CS 2027 Online Test Series72 tests in the real GATE…

    2 min read
  • Features of C Programming Language

    Prerequisite – C Language Introduction C is a simple language made in 1972 by Dennis Ritchie. It’s for system programming. C has low-level memory access, basic keywords, good…

    1 min read
  • C Language Introduction

    C is a programming language developed by Dennis Ritchie in 1972. It has a significant historical impact. It was originally created at Bell Laboratories of AT&T Labs for…

    4 min read