100% found this document useful (1 vote)
92 views6 pages

There Are 4 Main Types of Preprocessor Directives

Preprocessor directives allow code to be processed before compilation. There are four main types - macros, file inclusion, conditional compilation, and other directives. Macros allow code to be replaced by a name. File inclusion inserts other files. Conditional compilation includes or skips code based on conditions. Other directives include undefining macros and compiler-specific features. A C program goes through four phases before execution - preprocessing, compilation, assembly, and linking, where the preprocessor handles macros and file inclusion before compilation to machine code.

Uploaded by

drhemanand
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
92 views6 pages

There Are 4 Main Types of Preprocessor Directives

Preprocessor directives allow code to be processed before compilation. There are four main types - macros, file inclusion, conditional compilation, and other directives. Macros allow code to be replaced by a name. File inclusion inserts other files. Conditional compilation includes or skips code based on conditions. Other directives include undefining macros and compiler-specific features. A C program goes through four phases before execution - preprocessing, compilation, assembly, and linking, where the preprocessor handles macros and file inclusion before compilation to machine code.

Uploaded by

drhemanand
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Pre-processor directives:

As the name suggests Preprocessors are programs that process our source
code before compilation. There are a number of steps involved between
writing a program and executing a program in C / C++. Let us have a look at
these steps before we actually start learning about Preprocessors.

C Program

CompilerObject Code

Linker Executable Code

There are 4 main types of preprocessor directives:


1. Macros
2. File Inclusion
3. Conditional Compilation
4. Other directives
Let us now learn about each of these directives in detail.
 Macros: Macros are a piece of code in a program which is given some name. Whenever
this name is encountered by the compiler the compiler replaces the name with the actual
piece of code. The ‘#define’ directive is used to define a macro. Let us now understand
the macro definition with the help of a program:
#include <stdio.h>
// macro definition
#define LIMIT 5
int main()
{
for (int i = 0; i < LIMIT; i++) {
printf("%d \n",i);
}

return 0;
}
Output:
0
1
2
3
4
In the above program, when the compiler executes the word LIMIT it replaces it with 5.
The word ‘LIMIT’ in the macro definition is called a macro template and ‘5’ is macro
expansion.
Note: There is no semi-colon(‘;’) at the end of macro definition. Macro definitions do not
need a semi-colon to end.
Macros with arguments: We can also pass arguments to macros. Macros defined with
arguments works similarly as functions. Let us understand this with a program:

// macro with parameter


#define AREA(l, b) (l * b)
int main()
{
int l1 = 10, l2 = 5, area;
area = AREA(l1, l2);
printf("Area of rectangle is: %d", area);
return 0;
}
Output:
Area of rectangle is: 50
We can see from the above program that, whenever the compiler finds AREA(l, b) in the
program it replaces it with the statement (l*b) . Not only this, the values passed to the
macro template AREA(l, b) will also be replaced in the statement (l*b). Therefore
AREA(10, 5) will be equal to 10*5.
 File Inclusion: This type of preprocessor directive tells the compiler to include a file in
the source code program. There are two types of files which can be included by the user
in the program:
 Header File or Standard files: These files contains definition of pre-
defined functions like printf(), scanf() etc. These files must be included for
working with these functions. Different function are declared in different
header files. For example standard I/O functions are in ‘iostream’ file
whereas functions which perform string operations are in ‘string’ file.
Syntax:
 #include< file_name >
where file_name is the name of file to be included. The ‘<‘ and ‘>’ brackets tells the
compiler to look for the file in standard directory.
 user defined files: When a program becomes very large, it is good practice to divide it
into smaller files and include whenever needed. These types of files are user defined
files. These files can be included as:
#include"filename"
 Conditional Compilation: Conditional Compilation directives are type of directives
which helps to compile a specific portion of the program or to skip compilation of some
specific part of the program based on some conditions. This can be done with the help
of two preprocessing commands ‘ifdef‘ and ‘endif‘.
Syntax:
#ifdef macro_name
statement1;
statement2;
statement3;
.
.
.
statementN;
#endif
If the macro with name as ‘macroname‘ is defined then the block of statements will execute
normally but if it is not defined, the compiler will simply skip this block of statements.

 Other directives: Apart from the above directives there are two more directives which
are not commonly used. These are:
 #undef Directive: The #undef directive is used to undefine an existing
macro. This directive works as:
#undef LIMIT
Using this statement will undefine the existing macro LIMIT. After this statement every
“#ifdef LIMIT” statement will evaluate to false.
 #pragma Directive: This directive is a special purpose directive and is used to turn on
or off some features. This type of directives are compiler-specific, i.e., they vary from
compiler to compiler. Some of the #pragma directives are discussed below:
 #pragma startup and #pragma exit: These directives helps us to specify the
functions that are needed to run before program startup( before the control
passes to main()) and just before program exit (just before the control returns
from main()).
Note: Below program will not work with GCC compilers.
Look at the below program:
#include <stdio.h>
void func1();
void func2();
#pragma startup func1
#pragma exit func2
void func1()
{
printf("Inside func1()\n");
}
void func2()
{
printf("Inside func2()\n");
}
int main()
{
void func1();
void func2();
printf("Inside main()\n");
return 0;
}
Output:
Inside func1()
Inside main()
Inside func2()
The above code will produce the output as given below when run on GCC compilers:
Inside main()
 This happens because GCC does not supports #pragma startup or exit.
 #pragma warn Directive: This directive is used to hide the warning message which are
displayed during compilation.

 We can hide the warnings as shown below:


 #pragma warn -rvl: This directive hides those warning which are raised
when a function which is supposed to return a value does not returns a value.
 #pragma warn -par: This directive hides those warning which are raised
when a function does not uses the parameters passed to it.
 #pragma warn -rch: This directive hides those warning which are raised
when a code is unreachable. For example: any code written after
the return statement in a function is unreachable.
Compiler converts a C program into an executable. There are four phases for a C program
to become an executable:

1. Pre-processing
2. Compilation
3. Assembly
4. Linking
By executing below command, We get the all intermediate files in the current directory
along with the executable.
The following screenshot shows all generated intermediate files.

Let us one by one see what these intermediate files contain.


Pre-processing
This is the first phase through which source code is passed. This phase include:
 Removal of Comments
 Expansion of Macros
 Expansion of the included files.
 Conditional compilation
The preprocessed output is stored in the filename.i. Let’s see what’s inside filename.i:
using $vi filename.i
In the above output, source file is filled with lots and lots of info, but at the end our code is
preserved.
Analysis:
 printf contains now a + b rather than add(a, b) that’s because macros have expanded.
 Comments are stripped off.
 #include<stdio.h> is missing instead we see lots of code. So header files has been
expanded and included in our source file.
Compiling
The next step is to compile filename.i and produce an; intermediate compiled output
file filename.s. This file is in assembly level instructions. Let’s see through this file
using $vi filename.s
The snapshot shows that it is in assembly language, which assembler can understand.
Assembly
In this phase the filename.s is taken as input and turned into filename.o by assembler. This
file contain machine level instructions. At this phase, only existing code is converted into
machine language, the function calls like printf() are not resolved. Let’s view this file
using $vi filename.o

Linking
This is the final phase in which all the linking of function calls with their definitions are
done. Linker knows where all these functions are implemented. Linker does some extra
work also, it adds some extra code to our program which is required when the program
starts and ends. For example, there is a code which is required for setting up the
environment like passing command line arguments. This task can be easily verified by
using $size filename.o and $size filename. Through these commands, we know that how
output file increases from an object file to an executable file. This is because of the extra
code that linker adds with our program.

You might also like