Preprocessor Directives in C
Preprocessor Directives in C
DIRECTIVES
ARCHANA MENON P
archananirmal1414@gmail.com
Agenda
• Preprocessor
• Preprocessor Directives
• Macro Substitution
Simple macro substitution
Argumented macro substitution
Nested macro substitution
• Undefining a macro
• File inclusion directives
• Compiler control directives
• References
Preprocessor
• One of the unique feature of C language is preprocessor.
• The preprocessor is a program that processes the source code before it passes through
compiler. i.e., the C preprocessor executes before a program is compiled.
• Some actions it performs are
• inclusion of other files in the file being compiled
• definition of symbolic constants and macros
• conditional compilation of program code
main()
{
printf("%f",PI);
}
OUTPUT:
3.14000
b. Macros with Arguments
• The preprocessor permits us to define more complex and more useful form of replacements.
• General Form:
#define identifier(f1, f2,… fn) string
• The identifiers f1, f2,…,fn are the formal macro arguments.
• Subsequent occurrence of macro with arguments is known as macro call.
• When a macro is called, the preprocessor substitutes the string, replacing the formal
parameters with the actual parameters.
• Eg:-
#define CUBE(x) (x*x*x)
Volume=CUBE(side); (side*side*side);
void main()
{
printf("Minimum between 10 and 20 is: %d\n", MIN(10,20));
}
OUTPUT:
Minimum between 10 and 20 is: 10
c. Nesting of macro
• We can also use one macro within the definition of another macro.
• i.e., macro definitions may be nested.
• Eg:-
#define CIRCLE_AREA( x ) ( ( PI ) * ( x ) * ( x ) )
void main()
{
clrscr();
printf("Enter the number=");
scanf("%d",&n);
printf("\nThe square of %d is= %d",n,SQUARE(n));
printf("\nThe cube of %d is= %d",n,CUBE(n));
getch();
}
OUTPUT:
Enter the number=2
The square of 2 is= 4
The cube of 2 is= 8
Undefining a macro
•Macros can be discarded by using the #undef preprocessor directive.
•General form:
#undef identifier
•The scope of a symbolic constant or macro is from its definition until it is undefined with
#undef, or until the end of the file.
•A macro must be undefined before being redefined to a different value.
We want to ensure that the macro TEST is always When want the macro TEST never to be
defined, irrespective of whether it has been defined defined.
in the header file or not.
#include “DEFINE.h”
#include “DEFINE.h”
#ifdef TEST
#ifndef TEST
#define TEST 1 #undef
#endif #endif
…….. ……..
3. Compiler Control Directives
(Conditional Compilation)
Situation II
Suppose a customer has two different types of computers and you are required to write a
program that will run on both the systems. You want to use the same program, although
certain lines of code must be different for each system.
Here, we will make the program portable.
……
#else If we want the program to run on IBM PC,
main() include the directive,
{ {
…
#define IBM_PC
….
#ifdef IBM_PC … in the program; otherwise we don’t (then the
compiler compiles code for HP machine).
{ }
… #endif
… ……..
} }
3. Compiler Control Directives
(Conditional Compilation)
Situation III
You are developing a program (sales analysis) for selling in the open market. Some customers may insist on
having certain additional features. However, you would like to have a single program that would satisfy both
types of customers.
Similar to Situation II
#ifdef ABC
Group-A lines are included if the customer
group-A lines ABC is defined. Otherwise, Group-B lines are
included.
#else
group-B lines
#endif
3. Compiler Control Directives
(Conditional Compilation)
Situation IV
Suppose you are in the process of testing your program, which is rather a large one. You
would like to have print calls inserted in certain places to display intermediate results and
messages in order to trace the flow of execution and errors, if any. Such statements are called
‘debugging’ statements. You want these statements to be a part of the program and to become
‘active’ only when you decide so.
Debugging and testing are done to detect errors in the program. While the compiler can detect
syntactic and semantic errors, it cannot detect a faulty algorithm where the program executes,
but produces wrong results.
Debugging statements are not required once the errors are isolated and corrected. We can
either delete all of them, alternatively, make them inactive using control directives.
3. Compiler Control Directives
(Conditional Compilation)
Situation IV
…
#ifdef TEST …
#ifdef TEST
{
{
printf(“Array Elements\n”);
for(i=0; i<m; i++) printf(“…”);
printf(“x[%d] = %d\n”, i , x[i]); ….
} }
#endif
… #endif
… …
#ifdef TEST
{
The statements between the directives #ifdef and #endif are
printf(“Array Elements\n”);
included only if the macro TEST is defined. Once everything is OK,
for(i=0; i<m; i++) delete or undefine the TEST. This makes the #ifdef TEST conditions
printf(“x[%d] = %d\n”, i , x[i]); false and therefore all the debugging statements are left out.
}
#endif
/* Program to display current date and time using library functions*/
• The C library function ctime() returns a string representing the local time based on the
argument timer.
• The returned string has the following format: Www Mmm dd hh:mm:ss yyyy.
• time_t is a variable in time.h which is suitable for storing the calendar time.
• time() encode the time_t format and calculates the current calendar time.
• Here Www is the weekday, Mmm the month in letters, dd the day of the
month, hh:mm:ss the time and yyyy the year.
#include<stdio.h>
#include<time.h>
int main() OUTPUT:
{ This program has been executed at
time_t t; // not a primitive datatype (date and time):
time(&t); Mon Jan 18 11:10:55 2021
printf("\nThis program has been executed at (date and time): %s", ctime(&t));
return 0;
}
References
• E.Balaguruswamy
THANK YOU