C Preprocessor and Macros

The C preprocessor directives allows transformation of code before compilation. All preprocessing directives starts with the # symbol.

#define Preprocessor

#define preprocessor allows you to define a macro. A macro is a piece of code to which you can assign a name. Before compilaltion, the compiler replaces the name with the macro body wherever it has been introduced.

Syntax

#define N 20

Macros can also made to work like functions. This is known as function-like macros.

Code

#include <stdio.h>
#define AREA(a) a*a

int main(void)
{
    // AREA(a) is replaced by a*a
    printf("Area of square of side 5 cm is %d sq. cm.\n",AREA(5));
    return 0;
}

Output

Area of square of side 5 cm is 25 sq. cm.

#include Preprocessor

The #include preprocessor allows you to include header files to your C program. Before compilation, the compiler replaces the preprocessor with the body of the header file. Some commonly used header files in C are stdio.h, string.h and stdlib.h.

Syntax

// Include header files from system's include directory
#include<stdio.h>

// Include header files from current directory
#include"header.h"

// Include header files from a nested directory
#include"headers/header.h"

C Code

// Including header file stdio.h for I/O Operations
#include <stdio>
int main(void)
{
    printf("#Include preprocessor demo\n");
    return 0;
}

Preprocessed Code

// Body of stdio.h
// stdio.h may contain other header files
// they are also included
// file name macros.c

# 1 "macros.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "macros.c"
# 1 "c:\\mingw\\include\\stdio.h" 1 3
# 38 "c:\\mingw\\include\\stdio.h" 3
       
# 39 "c:\\mingw\\include\\stdio.h" 3
# 56 "c:\\mingw\\include\\stdio.h" 3
# 1 "c:\\mingw\\include\\_mingw.h" 1 3
# 55 "c:\\mingw\\include\\_mingw.h" 3

...

# 2 "macros.c" 2


# 3 "macros.c"
int main(void)
{
    printf("#Include preprocessor demo\n");
    return 0;
}

Conditional Compilation

C allows programmers to conditionally include header files, i.e. you can instruct preprocessor whether to include a block of code or not. Conditional compilation can be achieved using #if, #ifdef, #ifndef, #defined, #else and #elseif.

#ifdef directive

It is used to define a piece of code if the provided macro exists before including the subsequent code in the compilation process.

#ifdef MACRO    
   // conditional codes
#endif

#ifndef directive

Sometimes, a header file may get re-defined if one header file includes another. This is prevented using #ifndef before including a header file.

#ifndef FILE
#define FILE

// the entire file

#endif /* FILE */

#if, #elif, #else directive

These directives allows programmers flexibility to include conditional code similar to the if, else if, else statements.

#if EXP
    // conditional codes if EXP is non-zero
#elif EXP2
    // conditional code if EXP2 is non-zero
#else
    // conditional code for else
#endif

Learners are suggested to visit the following site The C Preprocessors to get complete understanding of C preprocessors.

Trending

C Programming

Remember to follow community guidelines and don't go off topic.