[go: up one dir, main page]

0% found this document useful (1 vote)
3K views21 pages

Preprocessor Directives in C

The document discusses the C preprocessor and its directives. The preprocessor processes source code before compilation by performing tasks like file inclusion, macro substitution and conditional compilation. There are three categories of preprocessor directives - macro substitution directives like #define and #undef, file inclusion directives like #include, and compiler control directives like #ifdef and #endif for conditional compilation. Macro substitution allows identifiers to be replaced by predefined strings and comes in three forms - simple substitution of constants, argumented macros, and nested macros. File inclusion inserts the contents of other files. Conditional compilation allows certain code blocks to be included or excluded based on whether macros are defined.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (1 vote)
3K views21 pages

Preprocessor Directives in C

The document discusses the C preprocessor and its directives. The preprocessor processes source code before compilation by performing tasks like file inclusion, macro substitution and conditional compilation. There are three categories of preprocessor directives - macro substitution directives like #define and #undef, file inclusion directives like #include, and compiler control directives like #ifdef and #endif for conditional compilation. Macro substitution allows identifiers to be replaced by predefined strings and comes in three forms - simple substitution of constants, argumented macros, and nested macros. File inclusion inserts the contents of other files. Conditional compilation allows certain code blocks to be included or excluded based on whether macros are defined.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

C PRE-PROCESSOR

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

• Preprocessor directives begin with #.


• Pre processor directives are divided into 3 categories:
1. Macro substitution directives
2. File inclusion directives
3. Compiler control directives
Preprocessor Directives
Directive Function
#define Defines a macro substitution
#undef Undefines a preprocessor macro
#include Inserts a particular file
#ifdef Returns true if this macro is defined
#ifndef Returns true if this macro is not defined
#if Tests if a compile time condition is true
#else The alternative for #if
#endif Specifies the end of #if
1. Macro Substitution
• Macro substitution is a process where an identifier in a program replaced by predefined string
composed of one or more tokens.
• It is achieved by #define directive.
• This statement is also called macro definition (or macro).
• General form:
#define identifier string
• The different forms of macro substitution are:
a. Simple macro substitution
b. Argumented macro substitution
c. Nested macro substitution

a. Simple Macro Substitution
• Simple string replacement is commonly used to define constants.
#define PI 3.14159
replaces all subsequent occurrences of the symbolic constant PI with the numeric constant
3.14159
• Eg:-
#define COUNT 100
#define CAPITL “DELHI”
#define SIZE sizeof(int)*4
#define TEST if(x>y)
#define AND
#define PRINT printf(“Very Good”);
TEST AND PRINT if(x>y) printf(“Very Good”);
Example Program1
#include <stdio.h>
#define PI 3.1415

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);

#define CUBE(x) ((x)*(x)*(x))


Volume=CUBE(a+b); ((a+b)*(a+b)*(a+b));
Example Program2
#include <stdio.h>
#define MIN(a,b) ((a)<(b)?(a):(b))

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 ) )

The statement: area = CIRCLE_AREA( 4 );


is expanded to: area = ( ( 3.14159 ) * ( 4 ) * ( 4 ) );

#define MAX(a,b) ((a)>(b)?(a):(b))


MAX(x,MAX(y,z)); gives the maximum of three values x,y and z
Example Program3
#include<stdio.h>
#define SQUARE(x) ((x)*(x))
#define CUBE(x) (SQUARE(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.

• Once undefined, a name can be redefined with #define.


2. File Inclusion
• An external file containing functions or macro definitions can be included as a part of a
program so that we need not rewrite those functions or macro definitions.
• General form:
#include "filename“
OR
#include <filename>
• The preprocessor inserts the entire entire contents of filename into the source code of the
program.
• When the filename is included within the double quotation marks, the search for the file is
made first in the current directory and then in the standard directories.
• Otherwise, the file is searched only in the standard directories.
• Nesting of included files is allowed.
• If an included file is not found, an error is reported.
3. Compiler Control Directives
(Conditional Compilation)
Situation I
You have included a file containing some macro definitions. It is not known whether a
particular macro has been defined in that header file. However you want to be certain that it is
defined (or not defined).

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

You might also like