0% found this document useful (0 votes)
1 views5 pages

PSPC Unit IVb Preprocessors

This document outlines the use of preprocessors in C programming, including directives such as #include and #define for creating symbolic constants and macros. It explains conditional compilation and the use of operators like # and ## for string manipulation and token concatenation. The content is aimed at B Tech/B Tech (Hons.) CSE students in their first semester.

Uploaded by

gdrivee515
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)
1 views5 pages

PSPC Unit IVb Preprocessors

This document outlines the use of preprocessors in C programming, including directives such as #include and #define for creating symbolic constants and macros. It explains conditional compilation and the use of operators like # and ## for string manipulation and token concatenation. The content is aimed at B Tech/B Tech (Hons.) CSE students in their first semester.

Uploaded by

gdrivee515
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/ 5

10/27/2023

Dr. Dileep Kumar Singh


Head, JLU-SOET
dileep.singh@jlu.edu.in

Problem Solving and Program


Design in C

Problem Solving and Program Design in C


UNIT –IV

B Tech/ B Tech (Hons.) CSE – 1st Sem.

Preprocessors in C

Outline
• Introduction
• The #include Preprocessor Directive
• The #define Preprocessor Directive: Symbolic Constants
• The #define Preprocessor Directive: Macros
• Conditional Compilation
• The # and ## Operators

Dr. Dileep Kumar Singh 1


10/27/2023

Introduction

• Preprocessing
– Occurs before a program is compiled
– Inclusion of other files
– Definition of symbolic constants and macros
– Conditional compilation of program code
– Conditional execution of preprocessor directives
• Format of preprocessor directives
– Lines begin with #
– Only whitespace characters before directives on
a line

The #include Preprocessor Directive


• #include
– Copy of a specified file included in place of the directive
#include <filename> -
• Searches standard library for file
• Use for standard library files
#include "filename"
• Use for user-defined files
• Used for
• Searches current directory, then standard library
– Loading header files (#include <iostream>)
– Programs with multiple source files to be compiled together
– Header file - has common declarations and definitions (classes,
structures, function prototypes)
• #include statement in each file

The #define Preprocessor Directive: Symbolic Constants

• #define
– Preprocessor directive used to create symbolic constants and
macros.
• Symbolic constants
– When program compiled, all occurrences of symbolic constant
replaced with replacement text
• Format
#define identifier replacement-text
– Example: #define PI 3.14159
– everything to right of identifier replaces text
#define PI = 3.14159
• replaces "PI" with " = 3.14159", probably results in an error
– Cannot redefine symbolic constants with more #define
statements

Dr. Dileep Kumar Singh 2


10/27/2023

The #define Preprocessor Directive: Macros

• Macro
– Operation defined in #define
– Macro without arguments: treated like a symbolic
constant
– Macro with arguments: arguments substituted for
replacement text, macro expanded
– Performs a text substitution - no data type checking

Example:
#define CIRCLE_AREA( x ) ( (PI) * ( x ) * ( x ) )

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

The #define Preprocessor Directive: Macros

• Use parenthesis
– Without them:
#define CIRCLE_AREA( x ) ((PI) * ( x ) * ( x ))
#define CIRCLE_AREA( x ) PI * x * x
area = CIRCLE_AREA( c + 2 );
becomes
area = 3.14159 * c + 2 * c + 2;
• Evaluates incorrectly
• Macor’s advantage is that avoiding function overhead
– Macro inserts code directly.
• Macro’s disadvantage is that its argument may be evaluated
more than once.

double circleArea ( double x )


{ return 3.1415926 * x * x ;
}

The #define Preprocessor Directive: Macros

• Multiple arguments
#define RECTANGLE_AREA( x, y ) ( ( x ) *
( y ) )
rectArea = RECTANGLE_AREA( a + 4, b + 7 );
becomes
rectArea = ( ( a + 4 ) * ( b + 7 ) );

Dr. Dileep Kumar Singh 3


10/27/2023

The #define Preprocessor Directive: Macros

• #undef
– Undefines a symbolic constant or macro, which
can later be redefined

• #define getchar() getc ( stdin )

10

Conditional Compilation
• Conditional compilation
– Control preprocessor directives and compilation
– Cast expressions, sizeof, enumeration constants
cannot be evaluated
• Structure similar to if
#if !defined( NULL )
#define NULL 0
#endif
– Determines if symbolic constant NULL defined
• If NULL is defined, defined(NULL) evaluates to 1
• If NULL not defined, defines NULL as 0
– Every #if ends with #endif
– #ifdef short for #if defined(name)
– #ifndef short for #if !defined(name)

11

Conditional Compilation (II)

• Other statements
#elif - equivalent of else if in an if
structure
#else - equivalent of else in an if structure

12

Dr. Dileep Kumar Singh 4


10/27/2023

The # and ## Operators

• #
– Replacement text token converted to string with quotes
#define HELLO( x ) printf (“ Hello, “ #x “\n”);

HELLO(John) Notice #
becomes
printf (“ Hello, “ “John” “\n”);
printf (“ Hello, John\n”);
• ##
– Concatenates two tokens
• Strings separated by whitespace are concatenated
#define TOKENCONCAT( x, y ) x ## y

TOKENCONCAT( O, K )
becomes
OK

13

THANKS

14

Dr. Dileep Kumar Singh 5

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy