C – Preprocessors3 min read

As the “pre” means beforehand, similarly it means processing something before passing it on further. The preprocessor is a program that processes the source program before it is passed to the compiler. So we can say that it is a separate process in the compilation.

Preprocessor commands (often known as directives) form what can almost be considered a language within the C language. We can certainly write C programs without knowing anything about the preprocessor or its facilities.

It gives specific features called directives. A preprocessor directive begins with “#” symbol at the starting of the program. These directives are processed before the compilation of the program.

The following are preprocessor directives:

  • Macro expansion
  • File inclusion
  • Conditional Compilation
  • Miscellaneous directives

Macro Definition

macro is defined by #define in a program. It is used to define some constant in a program that can be used anywhere during the entire program. This constant becomes global value.

#define PI 3.1415

This statement is called ‘macro definition’ or more commonly, just a ‘macro’.
Its purpose: during preprocessing, the preprocessor replaces every occurrence of PI in the program with a 3.1415 value.

You may try this program:


File inclusion

#include is used to include some files into the program. These are usually header files, but sometimes maybe any text file. The command looks something like this:

#include <stdio.h>

stdio.h is the header file that contains the library function that is used in a program such as printf, scanf, etc. #include is a preprocessor directive.

Check the example of macro mentioned above where stdio.h file is defined by #include. If we do not include this header file then we will not be able to use the in-built function.


Conditional Compilation

The C preprocessor provides a series of directives for conditional compilation: #if, #elif, #else, #ifdef, #ifndef, and #endif. These commands cause the preprocessor to include or exclude sections of the source code from compilation depending on certain conditions.

Conditional compilation is used for three main purposes:

  • Optionally include debugging code
  • Enclose non-portable code
  • Guard against multiple inclusion of header files.

Syntax:

#ifdef, #endif, #if, #else, #ifndef

#ifndef

It tells the C Preprocessor to define TEXT only if TEXT isn’t already defined.

#ifdef Directive

You can also add nested conditional to your #if...#else using #elif


Miscellaneous directives

#undef is used to undefine a defined macro variable.

It tells the C Preprocessor to undefine existing FILE_SIZE and define it as 38.

#pragma is used to call a function before and after the main function in a C program.

#undef, #pragma


Predefined Macros

MacroValue
__DATE__A string containing the current date
__FILE__A string containing the file name
__LINE__An integer representing the current line number
__STDC__If follows ANSI standard C, then the value is a nonzero integer
__TIME__A string containing the current date.

Example: print current time and date using predefined function.

You can run the above program in the compiler and the Current time and date will be displayed on the screen.


MORE

C Program to search an element in an array using Pointers

A separate function( search_function()) will be created where the array pointer will be declared and the searched element along with the size of an array …

C Program to find the sum of the digits of a number using recursion function

This C program calculates the sum of digits of a given number using recursion. Here’s a concise explanation: Function Definition: sumDigits(int n) This function calculates …

C program to find factorial of a number using Ternary operator with Recursion

Recursion refers to the function calling itself directly or in a cycle. Before we begin, you should have the knowledge of following in C Programming: …

C Program to Add Two Numbers Using Call by Reference

The program takes the two numbers from the user and passes the reference to the function where the sum is calculated. You may go through …

Find the output ab, cd, ef, g for the input a,b,c,d,e,f,g in Javascript and Python

In this tutorial, we will write a program to find a pairs of elements from an array such that for the input [a,b,c,d,e,f,g] we will …

String Pattern Programs in C

In this tutorial, we will write various C pattern programs for String. Before that, you may go through the following topics in C. for loop …