0% found this document useful (0 votes)
12 views23 pages

L9 Preprocessors in C

This document covers the C preprocessor, detailing its directives, functions, and usage in C programming. It explains the syntax and purpose of directives like #define, #undef, and conditional compilation directives, along with examples of macros and their applications. The content emphasizes the importance of understanding preprocessor directives for effective C programming and encourages students to refer to additional resources for comprehensive learning.

Uploaded by

Ishan
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)
12 views23 pages

L9 Preprocessors in C

This document covers the C preprocessor, detailing its directives, functions, and usage in C programming. It explains the syntax and purpose of directives like #define, #undef, and conditional compilation directives, along with examples of macros and their applications. The content emphasizes the importance of understanding preprocessor directives for effective C programming and encourages students to refer to additional resources for comprehensive learning.

Uploaded by

Ishan
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/ 23

C Programming

Module-9: The C Preprocessor

IMT and IMG 1st Year

Instructor : Dr. Santosh Singh Rathore


ABV-IIITM Gwalior
Disclaimer

This is NOT A COPYRIGHT MATERIAL

Content for the slides have been taken from the various sources and
from the reference books.

Students need to follow reference books to get the thorough details


of the concepts discussed in slides
Preprocessor
• The lines starting with the symbol # are known as preprocessor directives.
• When the preprocessor finds a line starting with the symbol #, it considers it as a
command for itself and works accordingly.
• All the directives are executed by the preprocessor, and the compiler does not
receive any line starting with symbol.
• Some features of preprocessor directives are-
• Each preprocessor directive starts with a # symbol.
• There can be only one directive on a line.
• There is no semicolon at the end of a directive.
• The preprocessor directives can be placed anywhere in a program (inside or
outside functions) but they are usually written at the beginning of a
program.
• A directive is active from the point of its appearance till the end of the
program.
Preprocessor
• The main functions performed by the preprocessor directives are-
• 1. Simple Macro Substitution
• 2. Macros with arguments
• 3. Conditional Compilation
• 4. Including files
• 5. Error generation, pragmas and predefined macro names.
• The preprocessor directives that perform these functions are as given
below-
• #define #else #error
• #if #elif #line
• #ifdef #endif #pragma
• #ifndef #undef
#define
• The general syntax is-
• #define macro name macro_expansion
• Here macro_name is any valid C identifier, and it is generally taken in capital
letters to distinguish from other variables.
• The macro_expansion can be any text. A space is necessary between the macro
name and macro expansion.
• The C preprocessor replaces all the occurrences of macro_name with the
macro_expansion. For example-
• #deflne TRUE 1
• #define FALSE 0
• #define PI 3.14159265
• #define MAX 100
• #define RETIRE AGE 58
#define
• C preprocessor searches for macro_name in the C source code and replaces it
with the macro_expansion.

• The C preprocessor searches for the macro name MSSG and substitutes the
message "I understand the use of #define\n" in the printf ().
#define
• The can have macro expansions of any sort. Here are some more examples-
• #define AND &&
• #define OR II
• #define BEGIN main(){
• #define END }
• #define INFINITE while(1)
• ~define NEW_LINE printf("\n");
• #define ERROR printf("An error has occurred\n");
• Here is a semicolon in the last two directives. This semicolon is just a part of
the macro_expansion.
• Now whenever we write NEW_LINE or ERROR in the program, they will be
replaced by printf calls and the terminating semicolon.
#define
• C preprocessor will simply replace the macro_name with the macro_expansion
and the macro_expansion can be any text. So it is our responsibility to see that
after replacement the resulting code is valid in C. For example if we define a
macro MAX like this-
• #define MAX 5;
• Now consider this declaration statement
• int arr[MAX];
• This will be translated by the preprocessor as
• int arr[5;]; and this is not valid in C.
• If the macro_name appears inside a character constant, string constant or a
comment then it is not replaced and is left as it is. For example-
• #define SUN 2
• #define WINDOWS 3
• printf("Rays of SUN are coming through the WINDOWS");
• The #define directive can also be used to define macros with arguments. The general
syntax is-
• #define macro_name(arg 1, arg2, ......) macro_expansion
• Here argl, arg2 .... are the fonnal arguments.
• The macro_name is replaced with the macro_expansion and the formal arguments are
replaced by the corresponding actual arguments supplied in the macro call.
Macros with Arguments
Macros with Arguments

• Some examples of macros with arguments-


• #define SQUARE(x) ( (x)*(x) )
• #define MAX( x, y ) ( (x) > (y) ? (x) : (y) )
• #define MIN( x, y ) ( (x) < (y) ? (x) : (y) J
• #define ISLOVvER(c) ( c >= 97 && c <= 122 )
• #define ISUPPER(c) ( c >= 65 && c <= 90 )
• #define TOUPPER(c) ( (c) + 'A' - 'a' )
• #define ISLEAP(y) ( (y%400 = = 0) II ( y%100!=0 && y%4 = = 0) )
• #define BLANK_LINES(n) { int i; fore i = 0; i < n; i++ ) printf("\n"); }
• #define CIRCLE_AREA(rad) ( 3.14 * (rad) *(rad) )
Nesting in Macros
Problems with Macros
Macros as Functions
Macros as Functions
#undef
• If we want to undefine a macro, we can use #undef
• Syntax: #undef macro_name
• After this directive if the macro_name is encountered in the program
then it will not be replaced by macro_exapnsion.
• In other words now the scope of the macro is limited to the code
between #define #Undef directives.
• This is used when we want the macro definition to be valid for only a
part of the program.
Conditional Compilation
• There may be situations when we want to compile some parts of the code
based on some condition.
• We know that before compilation the source code passes through the
preprocessor.
• So we can direct preprocessor to supply only some parts of the code to the
compiler for compilation.
• Conditional compilation means compilation of a part of code based on some
condition.
• These conditions are checked during the preprocessing phase. The directives
used in conditional compilation are-
• #ifdef #ifndef #if #else #elif #endif
#if and #endif
#else and #elif
#else and #elif
Predefine Macro Names
#error
Thank you for your attention!

< Questions?

You might also like