Preprocessor In C [Explained With Examples]
Hello friends, in today’s article we are going to talk about Preprocessor In C Language.
What is Preprocessor? To know this, first you need to know about the program execution cycle because there are many steps involved between writing a program and executing a program made in C, and without knowing these steps, you will not be able to understand the real importance of Preprocessor, so Let’s first know about these steps.
Source code -> Preprocessor -> Comiler -> Linker -> Executable Code (Software)
In this diagram, you have been shown the process cycle of the program.
This diagram describes how a source code (code written by a programmer is called source code, its extension is .c) is converted into Executable Code.
The preprocessor plays a very important role in this entire process. But now the question comes that, what is this preprocessor? And what is its use?
So now without wasting time let’s understand what is preprocessor In C.
What is Preprocessor In C
Preprocessor, as the name suggests, is a program that processes our source code before compilation.
The preprocessor scans and modifies the entire source code, and then passes that modified code to the compiler for compilation.
The preprocessor, while processing the source code, Replaces the header files (eg -: stdio.h, conio.h, string.h, maths.h etc.) and macros (eg -: #define, _TIME_ , _FILE_ etc.) with defined files and values.
Preprocessor interprets the code starting with #include means if we mention #include<stdio.h> header file in our code then preprocessor replaces the #include<stdio.h> code with the contents of the ‘stdio.h’ file. This is called pre-processing.
Lines starting with the # symbol are known as preprocessor directives.
When the preprocessor finds a line starting with the # symbol, it considers it a command for itself and acts accordingly.
Since all instructions beginning with # are executed by the preprocessor, the compiler does not receive any lines beginning with #.
Preprocessor directives have the following characteristics:
- Each Preprocessor directive begins with the # symbol.
- There can be only one Preprocessor directive on a line.
- There is no semicolon at the end of Preprocessor directives.
- Preprocessor directives can be placed anywhere in the program (inside a function or outside a function). But usually Preprocessor directives are written at the beginning of the program.
Types of Preprocessor Directives In C
Preprocessor Directives are divided into four parts -:
- Macros
- File Inclusion
- Conditional Compilation
- Other directives
Let’s now understand all these Preprocessor Directives one by one.
1) Macros -: #define
Macros are a type of identifiers that are usually written in capital letters to distinguish them from normal variables.
syntax -:
#define macro_name macro_expansion
For example -:
#deflne TRUE 1
#define FALSE 0
#define PI 3.14159265
#define MAX 100
#define RETIRE AGE 58
- #define is a preprocessor directive that defines macros.
- macro_expansion can be any text.
- There must be a space between macro_name and macro_expansion.
- The C preprocessor replaces all occurrences of macro_name with macro_expansion .
Let’s understand macros better through a program.
Example Program
#include <stdio.h>
// macro definition
#define LIMIT 10
int main()
{
int i;
for ( i = 0; i < LIMIT; i++)
{
printf("%d \n",i);
}
return 0;
}
In this program, I have defined a macro named LIMIT whose value is 10. Wherever this macro (LIMIT) is used in the program, the preprocessor will replace that macro name and put its value (10) in its place.
Output -:
0
1
2
3
4
5
6
7
8
9
Friends, if you want to know about macros in detail, then you can know well about macros from the link given below.
- Macro In C
Let us now talk about File Inclusion.
2) File Inclusion
#include, Preprocessor directive is used in C language to include the contents of the file in the source code.
With the help of #include, Preprocessor directive, we can include the content of two types of files in our source code -:
- Header File or Standard files
- User defined files
1) Header File or Standard files -:
Header File or Standard files are files that contain declaration of predefined functions.
There is a rule in C language according to which to use any function, first of all, we must declare that function. We cannot use that function in our program without declaring the function. And as we all know that there are two types of functions -:
- Predefined function
- User-defined function
Since predefined functions are already defined, we just have to declare them to use them in the program.
These predefined functions are declared in these header files, which we include with the help of #include, Preprocessor directive.
Syntax:
#include<file_name>
Here file_name is the name of the header file whose content we have to include in our source code.
Example
#include<stdio.h>
#include<conio.h>
This example contains instructions on how to include the contents of the stdio.h and conio.h header files in the source file, so as soon as the preprocessor sees this line, it will include the contents of these header files in the source code. These header files contain the declaration of predefined functions like printf(), scanf(), clrscr(), getch().
2) User-defined files
When the user or programmer creates a program, then he can create that program by dividing it into many parts. All these parts of the program are called user defined files. User can add the contents of these files anywhere in the program with the help of #include Preprocessor directive.
To include a user defined file in the source code, the syntax is written something like this –
Syntax -:
#include"filename"
Here instead of filename, the name of the file whose content you want to include in your source code will come.
Example -:
#include"C:\mydir\myfile.h"
Note -:
- The #include directive is used to include only the content of the header file in the source code. It cannot be used to include the contents of ‘.c’ files.
- Header files have an extension of .h so that it can be distinguished from other C files. However, this is not necessary.
- Header files generally contain macro definitions and declarations of enum, structure, union, typedef, external functions, and global variables.
- Angled brackets (< >) are used to include header files or standard files, and double quotes are used to include user-defined header files related to the program.
3) Conditional Compilation
Conditional Compilation directives are preprocessor directives that help to compile a particular part of the program or skip compilation of some particular part based on certain conditions.
#ifdef, #if, #defined, #else and #elseif Preprocessor Directive are used in Conditional Compilation.
Let us know about some of these preprocessor directives.
#ifdef Directive
The syntax of #ifdef directive is something like this -:
Syntax -:
#ifdef macroname
statement1;
statement2;
.
.
.
statementN;
#endif
If the macro is defined with the name ‘macroname’ then this block of statements will execute normally but, if it is not defined, the compiler will skip the statements in this block and will not compile them.
#defined
The #definition is used to check whether a certain macro is defined or not. It is often used with the #if directive.
Syntax -:
#if defined(macro_name)
Example -:
#if defined(FLAG)
If the macro FLAG is defined using #define, the expression defined(FLAG) will have a value of 1, otherwise, it will have a value of o.
Other directives
Apart from the above mentioned preprocessor directives, there are many other directives which I have listed below:
Preprocessor Directive | Description |
#error | To print error message |
#pragma | It is a special purpose directive which is used to provide additional information to the compiler. |
#undef | It is used to undefine preprocessor macros. |
#if | Used to check if condition is true or false |
#else | This is an alternative to #if. If the if condition becomes false then the statement after the else is executed. |
#elif | It is a combination of else if. |
#endif | It is used with #if at the end. |
#ifndef | It returns true if macro is not defined. |
Friends, if you want to know about the above mentioned preprocessor directive with detailed example, then tell in the comment below. If more people’s comments come, then I will write a separate article on these topics and tell all the things in detail because if I sit to tell all these in this one article, then the article will become very long.
Conclusion
Friends, I hope you have found the answer to your question and you will not have to search about what is preprocessor In C language. And why is preprocessor used in C language.
However, if you want any information related to this post or related to programming language, or computer science, then comment below I will clear your all doubts
If you want a complete tutorial on C language, then see here C Language Tutorial. Here you will get all the topics of C Programming Tutorial step by step.
Friends, if you liked this post, then definitely share this post with your friends so that they can get information about Preprocessor In C
To get the information related to Programming Language, Coding, C, C ++, subscribe to our website newsletter. So that you will get information about our upcoming new posts soon.