0% found this document useful (0 votes)
8 views26 pages

CTSD-2 Unit - 3

The document provides an overview of preprocessor directives in C programming, including file inclusion, macros, conditional compilation, and pragmas. It explains their functionalities, syntax, and provides examples for better understanding. Key directives such as #define, #include, #ifdef, and #pragma are highlighted with practical code snippets.

Uploaded by

ommahajan9890
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views26 pages

CTSD-2 Unit - 3

The document provides an overview of preprocessor directives in C programming, including file inclusion, macros, conditional compilation, and pragmas. It explains their functionalities, syntax, and provides examples for better understanding. Key directives such as #define, #include, #ifdef, and #pragma are highlighted with practical code snippets.

Uploaded by

ommahajan9890
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

303105151 -

Computational Thinking for


Structured Design-2
Dr. Harish Prajapati, Assistant Professor
AI & AIDS(PIET)
Scheme of the Subject
CHAPTER-3
Preprocessor Directives:
 File Inclusion
 Macros
 Conditional Compilation and
 Pragmas
Pre processor Directives
In almost every C program we come across, we see a few lines at
the top of the program preceded by a hash (#) sign. They are
called preprocessor directives and are preprocessed by the
preprocessor before actual compilation begins.
Preprocessor Directives Description
#define:Used to define a macro.
#undef:Used to undefine a macro.
#include:Used to include a file in the source code program.
#ifdef:Used to include a section of code if a certain macro is defined by
#define.
#ifndef:Used to include a section of code if a certain macro is not
defined by #define
Pre processor Directives
#include <stdio.h>
// define a macro
#define xyz 10
int main(){
// use conditional if-defined statement
// in this example, xyz is defined so we will execute
// the printf part of #ifdef
#ifdef xyz
printf("Your lottery number is %d.\n", xyz);
#else printf("error printing lottery number");
#endif
return 0;
}
Preprocessor Directives
#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.
#error:Used to generate a compilation error message.
#line:Used to modify line number and filename
information.
#pragma once:To make sure the header is included
only once.
#pragma message:Used for displaying a message
during compilation.
Example of Error Directive
#define BUFFER_SIZE 255
#if BUFFER_SIZE < 256
#error "BUFFER_SIZE is too small."
#endif
Example of line Directive
// line_directive.cpp
// Compile by using: cl /W4 /EHsc line_directive.cpp #include <stdio.h>
int main()
{
printf( "This code is on line %d, in file %s\n", __LINE__, __FILE__ );
#line 10
printf( "This code is on line %d, in file %s\n", __LINE__, __FILE__ );
#line 20 "hello.cpp"
printf( "This code is on line %d, in file %s\n", __LINE__, __FILE__ );
printf( "This code is on line %d, in file %s\n", __LINE__, __FILE__ ); }
Example of line Directive
This code is on line 7, in file line_directive.cpp
This code is on line 10, in file line_directive.cpp
This code is on line 20, in file hello.cpp
This code is on line 21, in file hello.cpp
Preprocessor Directives
Types of Preprocessor Directives in C
In C, preprocessor directives are categorized based on their
functionalities following are the types of preprocessor
directives:
Macro Definition
File Inclusion directive
Conditional Compilation
Line control
Error directive
Pragma directive
2.1File Inclusion Directive
 #include is one of the file inclusion directive in C.
 #include preprocessor directive is used to include the content
of one file to another file i.e. source code during the
preprocessing stage. This is done to easily organize the code
and increase the reusability of code.
 Syntax
 #include <file_name>
 or
 #include "filename"
File Inclusion Directive
Example:
#include <stdio.h> // including header file for Standard
input/output functions
#include <stdlib.h> // includng header file for Standard library
functions
int main()
{
// Using standard input/output functions from stdio.h
printf("Hello, Harish!\n");
// Using standard library functions
int num1 = 10, num2 = 5;
int sum = num1 + num2;
File Inclusion Directive
// displayng the result using printf from stdio.h
printf("Sum: %d\n", sum);
return 0;
}
Output
Hello, Harish!
Sum: 15
Macros
In C, macro definition directives uses the #define preprocessor
directive to define the macros and symbolic constants.
We use #define directive to define macro.
Macro are basically the symbolic names that represents lines of
code or some values.
This directive is used to create constants or to define short,
reusable codes .
Syntax
#define token value
Macros
// C program to illustrate the use of #define directive
#include <stdio.h>
// Defining a macro for PI
#define PI 3.14159
int main()
{
// Using the PI macro to calculate
double radius = 8.0;
double area = PI * radius * radius;
// Displaying the calculated area
printf("Area of the circle is: %f\n", area);
return 0;
}
Ans. Area of the circle is: 201.061760
2.3 Conditional Compilation
#if, #ifdef, #else, #elif, #endif
Conditional Compilation directives help to compile a specific
portion of the program or let us skip compilation of some
specific part of the program based on some conditions.

#ifdef: This directive is the simplest conditional directive.


This block is called a conditional group. The controlled text
will get included in the preprocessor output if the
macroname is defined. The controlled text inside a
conditional will embrace preprocessing directives.
Conditional Compilation
They are executed only if the conditional succeeds. You can nest these in
multiple layers, but they must be completely nested.
In other words, ‘#endif’ always matches the nearest ‘#ifdef’ (or ‘#ifndef’, or
‘#if’).
Also, you can’t begin a conditional group in one file and finish it in another.
Syntax

#ifdef MACRO
controlled text
#endif
Conditional Compilation
#ifndef: In #ifdef directive if the macroname is defined, then the block of
statements after the #ifdef directive will be executed normally but in case it is
not defined, the compiler will simply skip this block of statements.
The #ifndef directive is simply the opposite of #ifdef directive.
In case of #ifndef , the block of statements between #ifndef and #endif will
get executed only if the macro or the identifier with #ifndef is not defined.
Syntax
ifndef macro_name
statement1;
statement2;
.
.
statementN;
endif
Conditional Compilation
#if, #else and #elif: All these directives works together and control
compilation of portions of the program using some conditions. If the
condition with the #if directive results in a non zero value, then the group of
line immediately after the #if directive will be executed otherwise if the
condition with the #elif directive evaluates to a non zero value, then the
group of line immediately after the #elif directive will be executed else the
lines after #else directive will be executed.
Conditional Compilation
Syntax

#if macro_condition
statements
#elif macro_condition
statements
#else
statements
#endif
Conditional Compilation
// C program to demonstrate the use of conditional
// directives.
#include <stdio.h>
#define gfg 7
#if gfg > 200
#undef gfg
#define gfg 200
#elif gfg < 50
#undef gfg
#define gfg 50
#else
#undef gfg
#define gfg 100
#endif
Conditional Compilation
void printValue(int value) { printf("%d", value); }

int main()
{
printValue(gfg); // gfg = 50
return 0;
Pragma directive
The #pragma directive is used to provide the compiler some extra
information and hence it is compiler dependent so we can say that it’s
behavior vary according to the compiler we are using. The pragma directive
basically gives the instructions to the compiler to turn on or turn off some
feature and also to give some custom messages required.
Syntax

#pragma directive
Pragma directive
// C program to illustrate the use of Pragma to print the
// custom message
#include <stdio.h>
// not defining gfg to trigger pragma message
// #define GeeksforGeeks
int main()
{
#ifndef GeeksforGeeks
#pragma message(" GfG is not defined.")
#endif
printf("Hello geek!\n");
return 0;
}
Sol. #pragma message: GfG is not defined.
www.paruluniversity.ac.in

You might also like