There Are 4 Main Types of Preprocessor Directives
There Are 4 Main Types of Preprocessor 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
CompilerObject Code
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:
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.
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.
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.