TAMALE TECHNICAL UNIVERSITY
Department of Computer Science
Introduction to Computer
Programming II
Course code: CPA 112
Introduction
Preprocessors are programs that process the source code before
it passes through the compiler for compilation.
Several steps are involved between writing and executing a
program in C.
Let us look at these steps before we start learning about
Preprocessors.
Lesson Notes Prepared by Mahama, Haruna
Introduction cont’d
Lesson Notes Prepared by Mahama, Haruna
Introduction cont’d
Lesson Notes Prepared by Mahama, Haruna
Introduction cont’d
The source code written by programmers is first stored in a file, let
the name be “program.c“.
Preprocessors then process this file 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”.
Lesson Notes Prepared by Mahama, Haruna
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.
Syntax: # Preprocessor directives
Lesson Notes Prepared by Mahama, Haruna
Preprocessor Directives in C
C preprocessor allows users to define Macros, it is
therefore called Macro processor
A Macro is an open-ended subroutine ie it a set of
programming instructions simple replaces the text when call
preprocessor operates under the control of preprocessor
directives which begins with the # symbol
Lesson Notes Prepared by Mahama, Haruna
Lists all the preprocessor directives in C
Preprocessor Directives Description
#define Used to define a macro
#undef Used to undefine a macro
Used to include a file in the source code
#include
program
Used to include a section of code if a certain
#ifdef
macro is defined by #define
Used to include a section of code if a certain
#ifndef
macro is not defined by #define
#if Check for the specified condition
#else Alternate code that executes when #if fails
#endif Used to mark the end of #if, #ifdef, and #ifndef
Lesson Notes Prepared by Mahama, Haruna
Types of C Preprocessors Directive
There are 4 Main Types of Preprocessor
Directives:
1. Macros Substitution Directive
2. File Inclusion directives
3. Conditional Compilation / Compilation Control
Directive
4. Other directives such as ANSI C Additions to
preprocessor directive
Lesson Notes Prepared by Mahama, Haruna
Macro Substitution Directive
It replaces every occurrence o identifier by a
predefined string.
Whenever this identifier is encountered by the
compiler, the compiler replaces the identifier with the
actual predefined piece of code.
The ‘#define’ directive is used to define the macro.
Syntax of Macro Definition
#define identifier String
After preprocessing, the identifier will be replaced to its
predefined value in the program as shown below
Lesson Notes Prepared by Mahama, Haruna
Macro Substitution Directive
The 3 types of Macro substitution directives include:
1. Simple Macro Substitution directive
2. Macro substitution with argument directive
3. Nested Macro substitution
Lesson Notes Prepared by Mahama, Haruna
Siple Macro Substitution directive
This type of Macros is commonly used defined constant
Given below is the syntax
#define identifier string
E.g
# define NUMBER 100
# define TRUE 0
# define CITY “Tamale”
NOTE: All macros are written in capitals to identify them
as symbolic constant
Lesson Notes Prepared by Mahama, Haruna
// C Program to illustrate the macro
#include <stdio.h>
// macro definition
#define NUMBER 500
int main()
{
sum=NUMBER * val;
printf(“NUMBER = %d\n”, NUMBER);
return 0;
}
Lesson Notes Prepared by Mahama, Haruna
C Program to illustrate the macro cont’d
In the above program, when the compiler executes the word
NUMBER, it replaces it with 500.
The word ‘NUMBER’ in the macro definition is called a macro
template and ‘500’ is macro expansion.
Note
There is no semi-colon (;) at the end of the macro definition. Macro
definitions do not need a semi-colon to end.
Also, NUMBER in the quotation marks are not replaced
Lesson Notes Prepared by Mahama, Haruna
Macro Substitution with Arguments
Macros defined with arguments work similarly to functions. It
substitute the Formal Argument with the Actual argument
Generally
#define identifier (1,f2,f3,..,fn)
#define foo(a, b) a + b
#define func(r) r * r
Lesson Notes Prepared by Mahama, Haruna
// C Program to illustrate function like macros
#include <stdio.h>
// macro with parameter
#define AREA(l, b), (l * b)
int main()
{
int l1 = 10, l2 = 5, area;
area = AREA(l1, l2);
printf("Area of the rectangle is: %d", area);
return 0;
}
Output
Area of the rectangle is: 50
Lesson Notes Prepared by Mahama, Haruna
Macros With Arguments cont’d
When the above program runs, the compiler finds
AREA(l, b) in the program; it replaces it with the
statement (l*b).
Not only this, but 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 which
produced the result 50
Lesson Notes Prepared by Mahama, Haruna
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.
It is an external file containing functions. Macro definitions can be
included using the # include directive.
Syntax: #include “filename” It includes the current directory and
standard directory
Syntax: #include <filename> it includes only the standard directory
Lesson Notes Prepared by Mahama, Haruna
File Inclusion cont’d
Two types of files can be included by the user in the program
1. Standard Header Files
2. User-defined Header Files
Lesson Notes Prepared by Mahama, Haruna
File Inclusion cont’d
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 only.
Lesson Notes Prepared by Mahama, Haruna
File Inclusion cont’d
User-defined Header Files
When a program becomes very large, it is a good practice to divide it
into smaller files and include them whenever needed.
These types of files are user-defined header files.
Syntax
#include "filename"
The double quotes ( ” ” ) tell the compiler to search for the header
file in the source file’s directory and the standard directory.
Lesson Notes Prepared by Mahama, Haruna
Compiler Control Directives
These are used to control the compiler actions.
C preprocessor offers a feature called conditional compilation,
which can be used to switch on or off based on a particular line
or group of lines in a program.
For example
1. #if Directive
2. #ifdef Directive
3. #ifndef Directive
4. #else Directive
5. #elif Directive
6. #endif Directive
Lesson Notes Prepared by Mahama, Haruna
Compiler Control Directives cont’d
#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 executed if another_constant_expression is true
#else
// Code to be executed if none of the above conditions are
true
#endif
Lesson Notes Prepared by Mahama, Haruna
//program to demonstrates the use of #if, #elif, #else,
// and #endif preprocessor directives.
Output
PI is defined
Square is not defined
Lesson Notes Prepared by Mahama, Haruna
Other Directives
Apart from the above directives, two more directives 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 be evaluated as false.
Lesson Notes Prepared by Mahama, Haruna
Example
#include <stdio.h>
// defining MIN_VALUE
#define MIN_VALUE 10
int main() {
// Undefining and redefining MIN_VALUE
printf("Min value is: %d\n",MIN_VALUE);
//undefining min value
#undef MIN_VALUE
// again redefining MIN_VALUE
#define MIN_VALUE 20
printf("Min value after undef and again redefining it: %d\n",
MIN_VALUE);
return 0;
}
Lesson Notes Prepared by Mahama, Haruna
Example Cont’d
Output
Min value is: 10
Min value after undef and again redefining it: 20
Lesson Notes Prepared by Mahama, Haruna
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()).
Lesson Notes Prepared by Mahama, Haruna
End Lecture 1
Thank You
Any Question??
Lesson Notes Prepared by Mahama, Haruna