0% found this document useful (0 votes)
121 views

Preprocessor in C

The preprocessor in C is a program that processes source code before compilation by replacing macros and header files. It interprets lines starting with # as preprocessor directives. There are several types of directives including #define for macros, #include for file inclusion, and conditional compilation directives like #ifdef and #if that allow certain code blocks to be compiled or skipped based on conditions. The preprocessor scans the source code, modifies it by replacing macros and files, and passes the modified code to the compiler.

Uploaded by

sukhpreet singh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
121 views

Preprocessor in C

The preprocessor in C is a program that processes source code before compilation by replacing macros and header files. It interprets lines starting with # as preprocessor directives. There are several types of directives including #define for macros, #include for file inclusion, and conditional compilation directives like #ifdef and #if that allow certain code blocks to be compiled or skipped based on conditions. The preprocessor scans the source code, modifies it by replacing macros and files, and passes the modified code to the compiler.

Uploaded by

sukhpreet singh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Preprocessor in C

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 -> Compiler -> Linker -> Executable Code (Software)
Preprocessor in C
1. Types of Preprocessor Directives in C
a) Macros -: #define
2. File Inclusion
3. Conditional Compilation
a) #ifdef Directive
b) #defined
4. Other directives
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:
1. Each Preprocessor directive begins with the # symbol.
2. There can be only one Preprocessor directive on a line.
3. 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 -:
1. Macros
2. File Inclusion
3. Conditional Compilation
4. Other directives
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.
1. There must be a space between macro_name and macro_expansion.
2. The C preprocessor replaces all occurrences of macro_name with macro_expansion .
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
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 -:
1. Header File or Standard files
2. 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 -:
1. Predefined function
2. 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.

You might also like