Table of contents
1.
What are preprocessor Directives in C?
2.
Types of Preprocessor Directives
2.1.
1. Macros
2.2.
C
2.3.
C
2.4.
C
2.5.
2. File Inclusion
2.5.1.
Standard Header Files
2.5.2.
User Defined Header files
2.6.
3. Conditional Compilation
2.7.
C
2.8.
4. Other directives
2.8.1.
1. #undef Directive
2.9.
C
2.9.1.
2. #pragma directive
2.10.
C
3.
Preprocessor Operators
3.1.
Macro Continuation operator(\)
3.2.
C
3.3.
Stringize operator(#)
3.4.
C
3.5.
Defined() operator
3.6.
C
3.7.
Token Pasting Operator
3.8.
C
4.
Frequently Asked Questions
4.1.
Name some header files used in the C programming language.
4.2.
What is the difference between #pragma startup and #pragma exit?
4.3.
What will happen if we undefine a directive using #undef?
5.
Conclusion
Last Updated: Dec 30, 2024
Easy

Preprocessors in C

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Preprocessors are programs that are not a part of the compiler but are used by the compiler to process the source code before compilation. It is a text substitution tool that instructs the compiler to do a required pre-processing before the actual compilation, it is a separate step in the compilation process.

Preprocessors in C

Recommended Topic, Sum of Digits in C, C Static Function

What are preprocessor Directives in C?

Preprocessors in C are programs that provide preprocessors directives that tells the compiler to preprocess the source code before compiling. Preprocessor directives always begin with a “#” (hash) symbol. All the statements starting with the hash symbol go to the preprocessor program, and the preprocessor program will execute these statements. you can also use an online compiler editor.

Examples of preprocessor directives are #include, #define, etc. 

List of preprocessor directives in C are mentioned below:

DirectiveDescription
#defineDefines macros or constants to be replaced before compilation.
#undefUndefines a previously defined macro.
#includeIncludes the contents of a specified file (header file) into the source file.
#ifStarts a conditional compilation block based on a condition.
#ifdefCompiles the code that follows if the specified macro is defined.
#ifndefCompiles the code that follows if the specified macro is not defined.
#elseProvides an alternative block of code if the condition in #if or #ifdef fails.
#elifCombines #else and #if to check another condition if the previous #if failed.
#endifEnds the conditional compilation block started with #if, #ifdef, or #ifndef.
#errorGenerates a compilation error with a custom message if reached.
#pragmaIssues special commands to the compiler, often for controlling compiler behavior.
#lineChanges the line number and filename for the following lines of code.

Types of Preprocessor Directives

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

1. Macros

A macro is a segment of code in a program that is given some name, when they are encountered in the code by the compiler, the compiler replaces the name with the actual piece of code, which is usually a value. We use “#define” directive to define macros. Macro definitions do not end with a semicolon.

Example of Macros

  • C

C

#include <stdio.h>

// defining a macros
#define n 5
int main()
{
printf("Counting...");
for (int i = 1; i <= n; i++) {
 printf("\n%d ",i);
}

 return 0;
}
You can also try this code with Online C Compiler
Run Code

Output

Counting...
1 
2 
3 
4 
5 

 

Explanation

In the above code, when the compiler reads the word “n” it replaces it with its value defined in the starting code. The word “n” is referred to as macro template, and its value “5” is referred to as macro expansion.

We can define macros with arguments that work the same way functions do.

Example

  • C

C

#include <stdio.h>

// calc area of square
#define area(side) (side * side)
int main()
{
int side=10;
printf("Area of square is: %d", area(side));

return 0;
}
You can also try this code with Online C Compiler
Run Code

Output

Area of square is: 100


Explanation

In the above code, whenever the compiler finds area(side), it replaces it with the statement (side*side). The value passed as arguments is replaced with the variables, and the calculated value is displayed.

MacroDescription
__DATE__Set the current date as a character literal in "MMM DD YYYY" format.
__TIME__Sets the current time as a character literal in "HH:MM:SS" format.
__FILE__This contains the current filename as a string literal.
__LINE__This contains the current line number as a decimal constant.
__STDC__Defined as 1 when the compiler complies with the ANSI standard.

Example

  • C

C

#include <stdio.h>

int main() {

printf("File Name : %s\n", __FILE__ );
printf("Current Date : %s\n", __DATE__ );
printf("Current Time : %s\n", __TIME__ );
printf("Line : %d\n", __LINE__ );
printf("ANSI : %d\n", __STDC__ );

}
You can also try this code with Online C Compiler
Run Code

Output

File Name : test.c
Current Date : Mar 11 2022
Current Time : 12:27:45
Line : 8
ANSI : 1

2. File Inclusion

Whenever we want to include a file in the source code, we use the file inclusion type of preprocessor. Mainly two types of files can be added using this preprocessor:

Standard Header Files

Header files are the files that are added on the top of source code and contain pre-defined functions like printf(), scanf(), etc. Header files must be included to use these functions, different functions are declared in different header files.

Syntax

#include<file_name>


The angular brackets tell the compiler to look for the file with the mentioned file_name in the standard directory.

User Defined Header files

When working on a big project/program, the program is divided into smaller files that can be included whenever needed. This makes the program more readable and helps the user focus on a particular functionality. 

Syntax

#include "file_name"

3. Conditional Compilation

These are the directives that enable the user to compile a certain set of instructions or skip specific instructions based on some condition. Conditional compilation directives are declared using two preprocessing commands: “ifdef” and “endif”.
Following preprocessor directives that are used to insert conditional code:

  1. #if Directive
  2. #ifdef Directive
  3. #ifndef Directive
  4. #else Directive
  5. #elif Directive
  6. #endif Directive

Syntax

#ifdef macro_name
    // Code will execute if macro_name is defined
#ifndef macro_name
    // Code will execute if macro_name is not defined
#if constant_expr
    // Code will execute if constant_expr evaluates to true
#elif another_constant_expr
    // Code will execute if another_constant_expr evaluates to true
#else
    // Code will execute if none of the above conditions are true
#endif

Example

  • C

C

#include <stdio.h>

#define DEBUG
#define VERSION 2

int main() {

#ifdef DEBUG
printf("Debugging is enabled.\n");
#endif

#ifndef RELEASE
printf("Release mode is not enabled.\n");
#endif

#if VERSION == 1
printf("Version 1 of the software.\n");
#elif VERSION == 2
printf("Version 2 of the software.\n");
#else
printf("Unknown version.\n");
#endif

return 0;
}
You can also try this code with Online C Compiler
Run Code

Output

Debugging is enabled.
Release mode is not enabled.
Version 2 of the software.

4. Other directives

There are two more directives that are not commonly used in this directives:

  1. #undef Directive
  2. #pragma Directive

1. #undef Directive

This is used to undefine an existing macro.

Example 

  • C

C

#include <stdio.h>

#define MAX 9999999 // defining the MAX
#undef MAX // undefine the MAX

int main()
{
#ifdef MAX
 printf("MAX is defined, Value is: .\n", MAX);
#endif
 printf("MAX is not defined.\n");
}
You can also try this code with Online C Compiler
Run Code

Output

MAX is not defined.


Explanation

In the above code, we first define the MAX and then undefine it using the #undef. As a result, every “ifdef MAX” statement evaluates as false.  

2. #pragma directive

This directive is used to turn on/off some features, these directives are compiler-specific. Some of the #pragma directives are:

#pragma startup & #pragma exit

These directives are used to specify which functions are needed to run before control passes to main() and just before the control returns from main(). These directives do not work with GCC compilers as they don't support #pragma startup/exit.


Example 

  • C

C

#include <stdio.h>

void f1()
{
 printf("Before starting of main() function\n");
}

void f2()
{
 printf("Before ending of main() function()\n");
}

#pragma startup func1 // call before main()
#pragma exit func2 // call just before main() exits

int main()
{
 void f1();
 void f2();
 printf("Inside main()\n");

 return 0;
}
You can also try this code with Online C Compiler
Run Code

Output

Before starting of main() function
Inside main()
Before ending of main() function()

Also see, Tribonacci Series and Short int in C Programming

Preprocessor Operators

Macro Continuation operator(\)

The macro continuation operator (\) is confined to a single line. This is used to continue a macro that is too long for a single line.

Example

  • C

C

#include <stdio.h>
#define  helper  \
printf("Coding Ninjas Studio wishes you all the best!!!\n")

int main()
{
helper;
return 0;
}
You can also try this code with Online C Compiler
Run Code

Output

"Coding Ninjas Studio" wishes you all the best!!!

Stringize operator(#)

The stringize operator (#) is also known as the number-sign operator. This operator, when used within a macro definition, converts a macro parameter into a string constant. Usually used with macros having arguments.

Example

  • C

C

#include <stdio.h>
#define  helper(a)  \
printf(#a " wishes you all the best!!!\n")

int main()
{
helper("Coding Ninjas Studio");
return 0;
}
You can also try this code with Online C Compiler
Run Code

Output

"Coding Ninjas Studio" wishes you all the best!!!

Defined() operator

This operator is used in constant expressions to determine if an identifier is defined using #define, that is, if the specified identifier is defined, the value is true, if not defined, then the value is false.

Example

  • C

C

#include <stdio.h>

#if !defined (msg)
#define msg "Good Luck!"
#endif

int main(void) {
printf("Defined message: %s\n", msg); 
return 0;
}
You can also try this code with Online C Compiler
Run Code

Output

Defined message: Good Luck!

Token Pasting Operator

This is used to combine two arguments within a macro definition. It allows two tokens in the macro definition to be joined to form a single token. 

Example

  • C

C

#include <stdio.h>

#define tokenCombine(n) printf ("token" #n " = %d", token##n)

int main(void) {
int token10 = 10;
tokenCombine(10);
return 0;
}
You can also try this code with Online C Compiler
Run Code

Output

token10 = 10

Must Read, odd or even program in c

Frequently Asked Questions

Name some header files used in the C programming language.

Some of the commonly used header files in the C programming language are stdio.h, which has input/output functions, conio.h, which has console input/output functions, stdlib.h, which has general utility functions, etc.

What is the difference between #pragma startup and #pragma exit?

The main difference between #pragma startup and #pragma exit is that #pragma startup is used to specify the functions to be run before program startup, whereas #pragma exit is used to specify the functions that are to be run just before the program exits.

What will happen if we undefine a directive using #undef?

Whenever we undefine a directive using #undef, all the “ifdef macro_name” statements will be evaluated as false after the “#undef macro_name” statement.

Conclusion

In this article, we have extensively discussed what a preprocessor is, the types of preprocessors are, and the different types of preprocessor operators in the C programming language, with examples and their implementation in Visual Studio Code.

We hope that this blog has helped you enhance your knowledge regarding preprocessors in the C programming language and if you would like to learn more, check out our articles on how decision making takes place in C, how functions work, and how can you pass a parameter to them, what are structures and how to declare them.

Live masterclass