Seminar 1
Seminar 1
Seminar 1
SEMINAR
CS3353-C PROGRAMMING AND DATA STRUCTURES
C Pre-processors
Preprocessors are programs that process the source code before
compilation. Several steps are involved between writing a program and
executing a program in C.
Let us have a look at these steps before we actually start learning about
Preprocessors.
You can see the intermediate steps in the above diagram. The source
code written by programmers is first stored in a file, let the name be
“program.c“.
This file is then processed by preprocessors and an expanded source
code file is generated named“program.i”.
This expanded file is compiled by the compiler and an object code file is
generated named “program.obj”.
Finally, the linker links this object code file to the object code of the
library functions to generate the executable file “program.exe”.
Preprocessor Directives in C
Preprocessor programs provide preprocessor directives that tell the
compiler to preprocess the source code before compiling.
All of these preprocessor directives begin with a ‘#’ (hash) symbol. The
‘#’ symbol indicates that whatever statement starts with a ‘#’ will go to the
preprocessor program to get executed.
We can place these preprocessor directives anywhere in our program.
To better understand how preprocessors can enhance the efficiency of your
programs, especially when working with data structures,
Examples of some preprocessor directives are
#include, #define, #ifndef, etc.
Note Remember that the # symbol only provides a path to the preprocessor,
and a command such as include is processed by the preprocessor program.
For example, #include will include the code or content of the specified file in
your program.
Types of C Preprocessors
There are 4 Main Types of Preprocessor Directives:
1. Macros
2. File Inclusion
3. Conditional Compilation
4. Other directives
1. Macros
In C, Macros are pieces of code in a program that 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.
Syntax of Macro Definition
#define token value
where after preprocessing, the token will be expanded to its value in the
program.
#include <stdio.h>
// macro definition
#define LIMIT 5
int main()
return 0;
}
2. File Inclusion
This type of preprocessor directive tells the compiler to include a file in
the source code program. The #include preprocessor directive is used to
include the header files in the C program.
There are two types of files that can be included by the user in the
program:
Standard Header Files
The standard header files contain definitions of pre-defined functions
like printf(), scanf(), etc. These files must be included to work with these
functions. Different functions are declared in different header files.
For example, standard I/O functions are in the ‘iostream’ file whereas
functions that perform string operations are in the ‘string’ file.
Syntax
#include <file_name>
where file_name is the name of the header file to be included. The ‘<‘ and ‘>’
brackets tell the compiler to look for the file in the standard directory.
3. Conditional Compilation
Conditional Compilation in C directives is a type of directive that helps to
compile a specific portion of the program or to skip the compilation of some
specific part of the program based on some conditions. There are the following
preprocessor directives that are used to insert conditional code:
1. #if Directive
2. #ifdef Directive
3. #ifndef Directive
4. #else Directive
5. #elif Directive
6. #endif Directive
#endif directive is used to close off the #if, #ifdef, and #ifndef opening
directives which means the preprocessing of these directives is completed.
Syntax
#ifdef macro_name
// Code to be executed if macro_name is defined
#ifndef macro_name
// Code to be executed if macro_name is not defined
#if constant_expr
// Code to be executed if constant_expression is true
#elif another_constant_expr
// Code to be excuted if another_constant_expression is true
#else
// Code to be excuted if none of the above conditions are true
#endif
4. Other Directives
Apart from the above directives, there are two more directives that are not
commonly used. These are:
1. #undef Directive
2. #pragma Directive
1. #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 as false.
2. #pragma Directive
This directive is a special purpose directive and is used to turn on or off some
features. These types of directives are compiler-specific, i.e., they vary from
compiler to compiler.
Syntax
#pragma directive
Some of the #pragma directives are discussed below:
1. #pragma startup: These directives help us to specify the functions that are
needed to run before program startup (before the control passes to main()).
2. #pragma exit: These directives help us to specify the functions that are
needed to run just before the program exit (just before the control returns
from main()).