Macros, Conditional Compilation and Preprocessor Directives in C/C++

In C programming Preprocessing is a compilation step that occurs before the compiler compiles the C source code. In this step the C program is preprocessed and all the statements including the “#” token are resolved. Let’s first discuss the use of # token in C.

Preprocessor Introduction

All the statements starting with the token # are known as preprocessors in C. These pre-processors are divided into further categories, one of which is including other files from the current working directory and the other is writing an inline function. The most commonly used preprocessors are listed below, to name a few:

  • #inlude
  • #define
  • Condition compilation

There are also some pre-defines preprocessors in C. Let’s first discuss these 3 with examples then we will have a little discussion on the pre-defined preprocessors.

#include preprocessor Directives

This preprocessor is used to include the standard files in the C code. This statement is also a preprocessor statement because of sign #. All these fie inclusions are resolved during the 1st step of compilation i.e. pre-processing of source code. In simple words, during the preprocessing step, all the # tokens are removed from the C code, and as the output, we get a # free code.

Example Code

#include<stdio.h>

int main (void)
{
	printf("Hello world\n");
}

In this example the inclusion of stdio.h (standard input output file) is a preprocessor. Now as we are done with the preprocessor lets’ discuss macros now.

#define Preprocessor

This is also a preprocessor because of the # token. But because of the word “define” these statements are referred to as macros. The way we define a macro is shown below,

#define MAX_SIZE 10

In the complete C source code wherever the word MAX_SIZE is written it will be replaced with 10 during the step of preprocessing. Macros are not only used for defining a number it can also be used to define some inline function as shown in the example below.

#define SUM(a, b) a+b

This macro will take in input two integers and will return the sum of these two integers  wherever it is called in the function. Example code of the use of macros is shown below.

Example Code

#include<stdio.h>
#define SUM(a, b) a+b
int main (void)
{
	printf("Sum of 1 and 2 is: %d\n", SUM(1,2));
}

In this example, the word SUM(1, 2) is replaced by 1+2 during preprocessing because of the macro defined at line 2.

Conditional compilation

Conditional compilation contains conditional statements i.e. if else elif with a pound (#) token. This # sign eliminates the portion before compilation if the condition of the statement does not satisfy. #ifdef preprocessor will be checking the defines, if that specific macro exists the code inside the #ifdef bock will be processed else this code will be skipped during preprocessing.

Similarly the conditional statement #ifndef will be processed if the macro it is checking does not exists. Example of conditional compilation is given below,

Example Code

#include<stdio.h> 
#define MAX_SIZE 10

int main (void)
{
	#ifdef MAX_SIZE
	{
		printf("Max size macro exist\n");
	}
	#else
	{
		printf("Max_size macro doesn't exists\n'");
	}
	#endif
	#ifndef MAX_SIZE 
	{
		printf("Max_size macro doesn't exists\n'");
	}
	#endif
}

Program Output

macros and preprocessing in c

Apart from these 3 conditional compilation preprocessors there is one more wiz “#elif” this is the short form of else if statement.

Predefined macros in C

Following is the list of predefined macros in C

  • __DATE__ : A string containing the current date.
  • __FILE__ : A string containing the file name.
  • __LINE__ : A string containing the current line number.
  • __TIME__ : A string containing current time.
  1. C Program Compilation steps & Example with GCC Linux
  2. Introduction to Pointers
  3. How to find an array size without using sizeof()
  4. Pointer Arithmetic, Pointer Size, and Pointer Type
  5. In which order function parameters are evaluated in C/C++?
  6. Scope of global and static variables in C/C++
  7. Difference between Structure and Union in C
  8. C Program to remove duplicate elements from an array
  9. Difference Between void main(), int main() and void main(void) in C/C++

Leave a Reply