Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is a preprocessor
3.
Types of preprocessor directives
3.1.
Macros
3.2.
File Inclusion
3.2.1.
Header Files
3.2.2.
User Defined files
3.3.
Conditional Compilation
3.4.
Other directives
3.4.1.
#undef Directive
3.4.2.
#pragma directive
4.
Preprocessor Operators
4.1.
Macro Continuation operator
4.2.
Stringize operator
4.3.
Defined() operator
4.4.
Token Pasting Operator
5.
Must Read, odd or even program in c
6.
FAQs
7.
Key Takeaways
Last Updated: Mar 27, 2024

Preprocessors in C

Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

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.

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

What is a preprocessor

These 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. 

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Types of preprocessor directives

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

#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;
}


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

#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;
}


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.

Example

#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__ );

}


Output

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

 

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:
 

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 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"


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”.
 

Syntax

#ifdef name_of_macro
Statement 1
Statement 2
.
.
Statement N
#endif


Other directives

#undef Directive

This is used to undefine an existing macro.

Example 

#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");
}


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.  


#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 

#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;
}


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

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

int main()
{
  helper;
  return 0;
}


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

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

int main()
{
  helper("Coding Ninjas Studio");
  return 0;
}


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

#include <stdio.h>

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

int main(void) {
  printf("Defined message: %s\n", msg);  
  return 0;
}


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

#include <stdio.h>

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

int main(void) {
  int token10 = 10;
  tokenCombine(10);
  return 0;
}


Output

token10 = 10

Must Read, odd or even program in c

FAQs

  1.  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.
     
  2. 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.
     
  3. 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.

Key Takeaways

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.

Do upvote our blog to help other ninjas grow. Happy Coding!”

Previous article
Introduction to Error Handling
Next article
Introduction to Go
Live masterclass