The C preprocessor directives are a separate step in the compilation process, not a compiler component.
A C Preprocessor is merely a text substitution tool instructing the compiler to perform necessary pre-processing before the compilation.
CPP will be used to refer to the C Preprocessor.
Every preprocessor command begins with a hash (#). It must be the first non-blank character, and a preprocessor directive should begin in the first column for readability.
Following is a list of all significant preprocessor directives.
List of Preprocessor Directives in C
Directive | Description |
---|---|
#define | replaces it using a preprocessor macro. |
#include | adds a specific header from another file. |
#undef | Undefines a preprocessor macro. |
#ifdef | gives the value true if the macro is defined. |
#ifndef | provides the answer true if the macro is not defined. |
#if | examines the truth of a compile-time condition. |
#else | The replacement for #if. |
#elif | In one sentence, use #else and #if. |
#endif | ends the conditional preprocessor. |
#error | stderr is used to print an error message. |
#pragma | uses a defined procedure to issue special directives to the compiler. |
What are preprocessors in C?
C preprocessors instruct the compiler to perform any necessary processing prior to compilation.
You already understand the meaning of “preprocessor” based on its name.
“Pre” means before and “processor” means to create. It is not a component of the compiler, but it is a distinct step in the compilation process.
Consider a C file named main.c with the extension.c.
This file is then processed by preprocessors and subsequently compiled to produce an object file with the extension.obj.
The obj file is then linked with standard library functions to produce a file with the extension.exe, which is then executed.
What are Preprocessor Directives in c
C Preprocessor directives – It instructs the compiler to perform source code preprocessing prior to compilation.
The primary application of preprocessor directives is as commands. All preprocessor directives in C are preceded by the hash/pound(#) sign.
Preprocessor directives can be used anywhere in the application. The preprocessor directives should be used at the start of the program, though.
Preprocessors in C Example
Examine the samples below to comprehend the different instructions.
#define MAX_ARRAY_LENGTH 45
This directive instructs the CPP to substitute 45 for any instances of MAX ARRAY LENGTH. To make constants easier to read, use #define.
#include <stdio.h> #include "header.h"
These instructions instruct the CPP to get the text from stdio.h from the System Libraries and add it to the currently open source file.
The following line instructs CPP to fetch header.h from the local directory and insert its contents into the currently open source file.
#undef FILE_SIZE #define FILE_SIZE 45
It instructs the CPP to redefine FILE SIZE as 45 instead than leaving it as is.
#ifndef MESSAGE #define MESSAGE "Preprocessor in C Language!" #endif
The CPP is instructed to define MESSAGE only if MESSAGE has not yet been defined.
#ifdef DEBUG /* Your debugging statements here */ #endif
If DEBUG is specified, it instructs the CPP to process the contained statements.
If you provide the gcc compiler the –DDEBUG flag when compiling, this is helpful.
With the help of this definition of DEBUG, debugging can be turned on and off as needed during compilation.
Types of Preprocessor in C
Preprocessor directives come in four different categories.
- Macros
- File Inclusion
- Conditional Compilation
- Other directives
Predefined Macros in C
Using macros, programmers can run sections of code included within the macro. The macro is given a name by a programmer.
Additionally, the compiler substitutes the provided name with the macro’s internal code when it comes across the given name. A macro is defined using the #define directive.
A variety of macros are defined by ANSI C. The predefined macros can all be used in programming, although they shouldn’t all be changed directly.
Macros | Description |
---|---|
__DATE__ | The current date in the format “MMM DD YYYY” as a character literal. |
__TIME__ | The current time in “HH:MM:SS” format as a character literal. |
__FILE__ | This is a string literal that includes the current filename. |
__LINE__ | This holds a decimal constant that represents the current line number. |
__STDC__ | When the compiler complies with the ANSI standard, this value is defined as 1. |
Preprocessor and File Handling in C
The compiler is instructed to include a file in the program code via this preprocessing command.
A user may upload two different sorts of files:
- Header or Standard files
- user defined files
Header or Standard files
These files include the printf() and scanf() routines. It contains predefined functions, to put it simply.
Different functions are contained in several header files. For instance, an iostream file contains input/output functions and a string file has functions for processing strings.
Syntax:
#include <file_name>
User-defined files
A programmer can create a custom header file to break up long lines of code into manageable chunks.
Syntax:
#include"filename"
Conditional Compilation in C
This preprocessor directive’s primary use is to compile or avoid compiling particular code segments depending on a set of circumstances.
How to use conditional compilation directives in C?
You can use two preparation techniques to put the conditional compilation directive into practice.
- ifdef
- endif
How does a preprocessor work in C?
The steps involved in preprocessing are as follows:-
- Filtering out comments – For a better understanding of the various processes that are included in the program, comments are used in C. During processing, the compiler removes the remarks.
- File Inclusion – It instructs the compiler to include specific header files in order for specific functions to be carried out. There are 2 methods to go about it:
- #include<filename.h>
- #include”filename.h”
- Macro Expansion – You may occasionally need to employ certain code blocks in a recursive function.
- Object-like macros – Does not take parameters.
- Function-like macros – Capable of taking parameters.
Summary
In C, there are four primary categories of preprocessor directives.
You can define macros in a preprocessor to change your program before it is compiled.
Using macros, programmers can run sections of code included within the macro.
A macro is defined using the #define directive. Preprocessor refers to doing anything before processing it.
About Next Tutorial
In the next following post, I’ll create a Header Files in C and try to explain its many components in full detail.
I hope you enjoy this post on What is Preprocessors in C Language with Examples, in which I attempt to illustrate the language’s core grammar.
PREVIOUS
NEXT