SlideShare a Scribd company logo
20
                 C PREPROCESSOR



20.1 INTRODUCTION

We have already seen many features provided by C language. Yet
another unique feature of the C language is the preprocessor. The C
preprocessor provides several tools that are not available in other
high–level languages. The programmer can use these tools to make
his program more efficient in all respect.

20.2 OBJECTIVES

After going through this lesson you will be able to

    explain preprocessor working
    explain the # define Directive
    define constants
    explain macros
    write the various directions such as # undef, #include, #fdef,
    #ifdef, #ifndef, # else, #if

20.3 HOW THE PREPROCESSOR WORKS

When you issue the command to compile a C program, the program
is run automatically through the preprocessor. The preprocessor is
310 :: Computer Applications



                        a program that modifies the C source program according to directives
                        supplied in the program. An original source program usually is stored
                        in a file. The preprocessor does not modify this program file, but
                        creates a new file that contains the processed version of the program.
                        This new file is then submitted to the compiler. Some compilers
                        enable the programmer to run only the preprocessor on the source
                        program and to view the results of the preprocessor stage. All
                        preprocessor directives begin with the number or sharp sign (#).
                        They must start in the first column, and no space is required between
                        the number sign and the directive. The directive is terminated not
                        by a semicolon, but by the end of the line on which it appears.

                        The C preprocessor is a collection of special statements called direc-
                        tives, that are executed at the beginning of the compilation process.
                        The #include and # define statements are preprocessor directives.

                        20.4 THE # DEFINE DIRECTIVE

                        The #define directive is used to define a symbol to the preprocessor
                        and assign it a value. The symbol is meaningful to the preprocessor
                        only in the lines of code following the definition. For example, if the
                        directive #define NULL 0 is included in the program, then in all
                        lines following the definition, the symbol NULL is replaced by the
                        symbol. If the symbol NULL is written in the program before the
                        definition is encountered, however, it is not replaced.

                        The # define directive is followed by one or more spaces or tabs and
                        the symbol to be defined. The syntax of a preprocessor symbol is the
                        same as that for a C variable or function name. It cannot be a C
                        keyword or a variable name used in the program; if it is so a syntax
                        error is detected by the compiler. For example, suppose a program
                        contains the directive

                               #define dumb 52
                        which in turn is followed by the declaration
                               int dumb;
                        This would be translated by the preprocessor into
                               int 52;

                        which would be rejected by the compiler. The preprocessor does
                        not check for normal C syntax, except for identifying quotation
                        marks. It merely substitutes symbols where it finds them. The sym-
C Preprocessor :: 311



bol being defined is followed by one or more spaces or tabs and a
value for the symbol. The value can be omitted, in which case the
symbol is defined but not given a value. If this symbol is used later
in the program, it is deleted without being replaced with anything.

If a # define directive does not fit on a single line, it can be contin-
ued on subsequent lines. All lines of the directives except the last
must end with a backslash(  ) character. A directive can be split
only at a point where a space is legal.

20.5 CONSTANTS

A common use for defined symbols is the implementation of named
constants. The following are examples of constants in C:

    23, ‘a’, “hello”
Any of these can be assigned to a defined preprocessor symbol:
    #define INTEGER 23
    #define     CHARACTER ‘a’

A defined symbol can specify only a complete constant. For example,
if a program contains the definition

    #define NULL 0

the number 120 cannot be represented by 12 NULL. A defined sym-
bol can be recognized only if it is delimited by white space, punc-
tuation or operators.

20.6 MACROS

When a constant is associated with a defined symbol, the characters
making up the constants are assigned , not the numeric value of the
constant. The definition

    #define EOF-1

really assigns the string “-1” to EOF. The preprocessor replaces the
symbol EOF by the characters “-1” without any consideration of the
meaning of the two characters.

When a preprocessor symbol is defined as a segment of text, it is
more generally called a macro. Macros are very useful in making C
code more readable and compact. For example, some programmers
include the following definitions in all their programs:
312 :: Computer Applications



                                # define and &&
                               # define or    ||

                        These definitions enable the programmer to write more readable
                        code, such as the following:

                               If (a<b or c>d and e<f)

                        A comment can be included at the end of a macro or constant defi-
                        nition:

                               # define same 1 /* Nothing */

                        A comment can be inserted at any point in a C program where white
                        space is permitted without adverse effect to the program.

                        Macros also can be used as abbreviation for lengthy and frequently
                        used statements. The one restriction on macros is that, even though
                        their definitions can occupy more than one line, they cannot include
                        a newline. That is, when a macro is expanded all the resulting text
                        is placed on the same line.

                        The preprocessor permits macros to have arguments, just as func-
                        tions do; the difference is that the arguments are strings rather
                        than values. Consider the following definition.

                               #define Decrement(x) if (x>0)x - = 1

                        when this macro is expanded, the string passed to the argument x
                        replaces all occurrences of x in the symbol’s value. That is, the state-
                        ment

                               Decrement(a);
                               is expanded to
                                    if(a>0) a- = 1;
                               and the statement
                               Decrement(b);
                               becomes
                                    if (b>0) b - =1;

                        Notice that the macro definition itself does not include a semicolon.
                        When the macro is invoked later, it is followed by an explicitly speci-
                        fied semicolon:
C Preprocessor :: 313



      Decrement(a);

The argument names used in a macro are local to the macro. Thus
there is no conflict between a macro’s formal parameter names and
identifiers used in the program itself. Any symbols used in the macro
definition which are not argument name or names of other macros
are left unchanged, and are assumed to be variable names or other
C symbols.

The argument supplied to a macro can be any series of characters.

The comma is the delimiter that separates arguments in a macro.

A macro can be defined in terms of another macro, as in the follow-
ing example of nested macros:

      #defined control “%dn”
      #define printint(x) printf(control,x)
      #define test(x) if (x>0) printint(x)

If a program contains the statements

      Test(w);
Where w is an integer variable, the statement goes through the
following conversion steps:

i)    if(w>0) printint(w);

ii)   if (w>0) printf(CONTROL,w);
iii) if(w>0) printf (“%dn,”w);

INTEXT QUESTIONS

1.    What, in general terms, is the role played by the C preproces-
      sor ?
2.    Which symbol always precedes preprocessor directives ?
3.    Where on the line must all preprocessor directives begin ?
4.    Where can a preprocessor directive be written ?
5.    Which symbol is used to separate the arguments of a macro ?
314 :: Computer Applications



                        20.7 USE OF DIRECTIVES: # undef, # include

                        (a)    The # undef Directive:-

                        If a preprocessor symbol has already been defined, it must be unde-
                        fined before being redefined. This is accomplished by the #undef
                        directive, which specifies the name of the symbol to be undefined. It
                        is not necessary to perform the readefinition at the beginning of a
                        program. A symbol can be redefined in the middle of a program so
                        that it has one value in the first part and another value at the end.

                               A symbol need not be redefined after it is undefined.

                        (b) The #include Directive:-

                        Often a programmer accumulates a collection of useful constants
                        and macro definitions that are used in almost every program. It is
                        desirable to be able to store these definitions in a file that can be
                        inserted automatically into every program. This task is performed
                        by the #include directive.

                        A file is inserted at a point where the #include directive is encoun-
                        tered. Such files are called header files, and by convention their
                        names end with the character.h (as in stdio.h).

                        20.8 CONDITIONAL COMPILATION: #ifdef, #ifndef and #endif

                        Removing statements by hand would be quite tedious and could
                        also lead to error. For this reason, the preprocessor provides directives
                        for selectively removing section of code. This process is known as
                        conditional compilation.

                        #define RECORD–FILE

                        If the # ifdef directive tests whether a particular symbol has been
                        defined before the #ifdef is encountered. It does not matter what
                        value has been assigned to the symbol. In fact, a symbol can be
                        defined to the preprocessor without a value.

                        If the #ifdef directive is encountered after this definition it produce a
                        true result. If, however, the directive #undef RECORD–FILE is
                        encountered before the directive #ifdef RECORD–FILE then the
                        preprocessor considers the symbol RECORD–FILE to be undefined
                        and the #ifdef directive returns a false value.
C Preprocessor :: 315



If an #ifdef returns a true value, all the lines between the #ifdef and
the corresponding #endif directive are left in the program. If those
lines contains preprocessor directives, the directives are processed.
In this way, conditional compilation directives can be nested. If the
#ifdef evaluates as false, the associated lines are ignored, including
any preprocessor directives that are included.

The statements need not all be grouped in one place. The #ifdef and
#fendif directives can be used as many times as required. The pre-
processor also provides the directive #ifndef, which produces a true
result if a symbol is not defined. This makes it possible to use a
single symbol to switch between two versions.

Conditional compilation can be used to select preprocessor direc-
tives as well as C code. For example suppose a header file is included
in a program. and a certain preprocessor symbol (say, FLAG) may or
may not be defined in that header file. If the programmer wants
FLAG never to be defined, then the #include directive can be followed
by

    #ifdef FLAG
    #undef FLAG
    #endif

This ensures that, even if the symbol is defined in the header file,
its definition is removed. It is not sufficient merely to work on

    #undef FLAG

because if FLAG is not defined, the directive is erroneous. If FLAG
should always be defined, then we would write

    #ifndef FLAG

     #define FLAG

     #endif

We could, of course, give FLAG a value. We cannot simply write

    #define FLAG

since if FLAG is already defined, an error probably will result.

20.9 THE #ELSE DIRECTIVE

This directive functions in much the same way as the else clause of
an if statement.
316 :: Computer Applications



                        All lines between an #ifdef or an #ifndef directive and the corre-
                        sponding # else clause are included if the #ifdef or #ifndef is true.
                        Otherwise, the lines between the #else and the corresponding #endif
                        are included.

                        The # else directive and the lines that follow it can be omitted, but
                        never the #endif directive. No other text can be included on the
                        same line as the #else or #endif directive.

                        20.10 THE # IF DIRECTIVE

                        The #if directive tests an expression. This expression can be of any
                        form used in a C program, with virtually any operators, except that
                        it can include only integer constant values No variables, or function
                        calls are permitted, nor are floating point, character or string con-
                        stants.

                        The #if directive is true, if the expression evaluates to true (non–
                        zero). Any undefined preprocessor symbol used in the #if expression
                        is treated as if it has the value φ. Using a symbol that is defined with
                        no value does not work with all preprocessors, and an attempt to do
                        so might result in an error.

                        INTEXT QUESTIONS

                        6.     What is the characteristics of the #else and the #endif directives?

                        7.     How is the #ifndef directive used?

                        8.     State the reason for using the #ifdef directive.

                        20.11 WHAT YOU HAVE LEARNT

                        In this lesson, you have learnt about preprocessors. The preprocessor
                        is a program which modifies the C source program according to
                        instructions provided. Remember that all preprocessor directives
                        begin with the symbol #. There are different preprocessor directives.
                        You also learnt about Macros. This lesson also discussed about
                        conditional compilation.

                        20.12 TERMINAL QUESTIONS

                        1.     How can a #define directive be continued to a new line ?
                        2.     Where can a preprocessor directive be split ?
C Preprocessor :: 317



3.   What is a macro, and what is it used for ?
4.   What can a macro argument consist of ?
5.   What role is played by the #undef directive?
6.   How is the # include directive used ?
7.   What is conditional compilation ?
8.   How does the #if directive operate ?

20.13 KEY TO INTEXT QUESTIONS

1.   It provides for the implementation of macros and conditional
     compilation.
2.   The # sign.
3.   The first column.
4.   Anywhere in a program, so long as it starts in the first column
     of a line and is not on the same line as another directive or C
     statement.
5.   The comma.

6.   They only allow that text can be included on the lines they
     occupy.

7.   It passes lines of code to the compiler only if the specificed
     preprocessor symbol is not defined.

8.   The #ifdef directive is used in order to pass certain lines of code
     of the compiter, only if the specified preprocessor symbol is
     defined before the #ifdef directive is encounted.

More Related Content

What's hot

C programming session7
C programming  session7C programming  session7
C programming session7
Keroles karam khalil
 
88 c-programs
88 c-programs88 c-programs
88 c-programs
Leandro Schenone
 
6 preprocessor macro header
6 preprocessor macro header6 preprocessor macro header
6 preprocessor macro header
hasan Mohammad
 
Module 05 Preprocessor and Macros in C
Module 05 Preprocessor and Macros in CModule 05 Preprocessor and Macros in C
Module 05 Preprocessor and Macros in C
Tushar B Kute
 
C programming interview questions
C programming interview questionsC programming interview questions
C programming interview questions
adarshynl
 
Preprocessor in C
Preprocessor in CPreprocessor in C
Preprocessor in C
Prabhu Govind
 
The future of DSLs - functions and formal methods
The future of DSLs - functions and formal methodsThe future of DSLs - functions and formal methods
The future of DSLs - functions and formal methods
Markus Voelter
 
delphi-interfaces.pdf
delphi-interfaces.pdfdelphi-interfaces.pdf
Password protected diary
Password protected diaryPassword protected diary
Password protected diary
SHARDA SHARAN
 
C tutorial
C tutorialC tutorial
C tutorial
Diwakar_singh1989
 
Preprocessor directives in c language
Preprocessor directives in c languagePreprocessor directives in c language
Preprocessor directives in c language
tanmaymodi4
 
Notes part5
Notes part5Notes part5

What's hot (15)

C programming session7
C programming  session7C programming  session7
C programming session7
 
88 c-programs
88 c-programs88 c-programs
88 c-programs
 
6 preprocessor macro header
6 preprocessor macro header6 preprocessor macro header
6 preprocessor macro header
 
Module 05 Preprocessor and Macros in C
Module 05 Preprocessor and Macros in CModule 05 Preprocessor and Macros in C
Module 05 Preprocessor and Macros in C
 
C programming interview questions
C programming interview questionsC programming interview questions
C programming interview questions
 
Preprocessor in C
Preprocessor in CPreprocessor in C
Preprocessor in C
 
pre processor directives in C
pre processor directives in Cpre processor directives in C
pre processor directives in C
 
Switch case and looping
Switch case and loopingSwitch case and looping
Switch case and looping
 
The future of DSLs - functions and formal methods
The future of DSLs - functions and formal methodsThe future of DSLs - functions and formal methods
The future of DSLs - functions and formal methods
 
Preprocessor
PreprocessorPreprocessor
Preprocessor
 
delphi-interfaces.pdf
delphi-interfaces.pdfdelphi-interfaces.pdf
delphi-interfaces.pdf
 
Password protected diary
Password protected diaryPassword protected diary
Password protected diary
 
C tutorial
C tutorialC tutorial
C tutorial
 
Preprocessor directives in c language
Preprocessor directives in c languagePreprocessor directives in c language
Preprocessor directives in c language
 
Notes part5
Notes part5Notes part5
Notes part5
 

Similar to Chapter 13.1.11

C programming session6
C programming  session6C programming  session6
C programming session6
Keroles karam khalil
 
Basic structure of c programming
Basic structure of c programmingBasic structure of c programming
Basic structure of c programming
TejaswiB4
 
Basic structure of c programming
Basic structure of c programmingBasic structure of c programming
Basic structure of c programming
TejaswiB4
 
Preprocessor directives in c laguage
Preprocessor directives in c laguagePreprocessor directives in c laguage
Preprocessor directives in c laguage
Tanmay Modi
 
Introduction to Preprocessors
Introduction to PreprocessorsIntroduction to Preprocessors
Introduction to Preprocessors
Thesis Scientist Private Limited
 
1 - Preprocessor.pptx
1 - Preprocessor.pptx1 - Preprocessor.pptx
1 - Preprocessor.pptx
AlAmos4
 
Unit 5 Part 1 Macros
Unit 5 Part 1 MacrosUnit 5 Part 1 Macros
Unit 5 Part 1 Macros
Arpana Awasthi
 
Inline functions & macros
Inline functions & macrosInline functions & macros
Inline functions & macros
Anand Kumar
 
05 -working_with_the_preproce
05  -working_with_the_preproce05  -working_with_the_preproce
05 -working_with_the_preproceHector Garzo
 
PreProcessorDirective.ppt
PreProcessorDirective.pptPreProcessorDirective.ppt
PreProcessorDirective.ppt
Osmania University
 
C programming
C programmingC programming
C programming
PralhadKhanal1
 
Unit-2.pptx
Unit-2.pptxUnit-2.pptx
Unit-2.pptx
sidhantkulkarni1
 
Bcsl 031 solve assignment
Bcsl 031 solve assignmentBcsl 031 solve assignment
Unit-1 (introduction to c language).pptx
Unit-1 (introduction to c language).pptxUnit-1 (introduction to c language).pptx
Unit-1 (introduction to c language).pptx
saivasu4
 
Preprocessor directives
Preprocessor directivesPreprocessor directives
Preprocessor directives
rabbianasir99
 
Introduction of C++ By Pawan Thakur
Introduction of C++ By Pawan ThakurIntroduction of C++ By Pawan Thakur
Introduction of C++ By Pawan Thakur
Govt. P.G. College Dharamshala
 
C++ by shantu
C++ by shantuC++ by shantu
C++ by shantu
Shant007
 
Unit 5 quesn b ans5
Unit 5 quesn b ans5Unit 5 quesn b ans5
Unit 5 quesn b ans5
Sowri Rajan
 
C and CPP Interview Questions
C and CPP Interview QuestionsC and CPP Interview Questions
C and CPP Interview Questions
Sagar Joshi
 

Similar to Chapter 13.1.11 (20)

ANSI C Macros
ANSI C MacrosANSI C Macros
ANSI C Macros
 
C programming session6
C programming  session6C programming  session6
C programming session6
 
Basic structure of c programming
Basic structure of c programmingBasic structure of c programming
Basic structure of c programming
 
Basic structure of c programming
Basic structure of c programmingBasic structure of c programming
Basic structure of c programming
 
Preprocessor directives in c laguage
Preprocessor directives in c laguagePreprocessor directives in c laguage
Preprocessor directives in c laguage
 
Introduction to Preprocessors
Introduction to PreprocessorsIntroduction to Preprocessors
Introduction to Preprocessors
 
1 - Preprocessor.pptx
1 - Preprocessor.pptx1 - Preprocessor.pptx
1 - Preprocessor.pptx
 
Unit 5 Part 1 Macros
Unit 5 Part 1 MacrosUnit 5 Part 1 Macros
Unit 5 Part 1 Macros
 
Inline functions & macros
Inline functions & macrosInline functions & macros
Inline functions & macros
 
05 -working_with_the_preproce
05  -working_with_the_preproce05  -working_with_the_preproce
05 -working_with_the_preproce
 
PreProcessorDirective.ppt
PreProcessorDirective.pptPreProcessorDirective.ppt
PreProcessorDirective.ppt
 
C programming
C programmingC programming
C programming
 
Unit-2.pptx
Unit-2.pptxUnit-2.pptx
Unit-2.pptx
 
Bcsl 031 solve assignment
Bcsl 031 solve assignmentBcsl 031 solve assignment
Bcsl 031 solve assignment
 
Unit-1 (introduction to c language).pptx
Unit-1 (introduction to c language).pptxUnit-1 (introduction to c language).pptx
Unit-1 (introduction to c language).pptx
 
Preprocessor directives
Preprocessor directivesPreprocessor directives
Preprocessor directives
 
Introduction of C++ By Pawan Thakur
Introduction of C++ By Pawan ThakurIntroduction of C++ By Pawan Thakur
Introduction of C++ By Pawan Thakur
 
C++ by shantu
C++ by shantuC++ by shantu
C++ by shantu
 
Unit 5 quesn b ans5
Unit 5 quesn b ans5Unit 5 quesn b ans5
Unit 5 quesn b ans5
 
C and CPP Interview Questions
C and CPP Interview QuestionsC and CPP Interview Questions
C and CPP Interview Questions
 

More from patcha535

Chapter 14.2
Chapter 14.2Chapter 14.2
Chapter 14.2patcha535
 
Chapter 14.1
Chapter 14.1Chapter 14.1
Chapter 14.1patcha535
 
Chapter 13.2 (homework)
Chapter 13.2 (homework)Chapter 13.2 (homework)
Chapter 13.2 (homework)patcha535
 
Chapter 13.2
Chapter 13.2Chapter 13.2
Chapter 13.2patcha535
 
Chapter 13.1.10
Chapter 13.1.10Chapter 13.1.10
Chapter 13.1.10patcha535
 
Chapter 13.1.9
Chapter 13.1.9Chapter 13.1.9
Chapter 13.1.9patcha535
 
Chapter 13.1.8
Chapter 13.1.8Chapter 13.1.8
Chapter 13.1.8patcha535
 
Chapter 13.1.7
Chapter 13.1.7Chapter 13.1.7
Chapter 13.1.7patcha535
 
Chapter 13.1.6
Chapter 13.1.6Chapter 13.1.6
Chapter 13.1.6patcha535
 
Chapter 13.1.5
Chapter 13.1.5Chapter 13.1.5
Chapter 13.1.5patcha535
 
Chapter 13.1.4
Chapter 13.1.4Chapter 13.1.4
Chapter 13.1.4patcha535
 
Chapter 13.1.3
Chapter 13.1.3Chapter 13.1.3
Chapter 13.1.3patcha535
 
Chapter 13.1.2
Chapter 13.1.2Chapter 13.1.2
Chapter 13.1.2patcha535
 
Chapter 13.1.1
Chapter 13.1.1Chapter 13.1.1
Chapter 13.1.1patcha535
 
Chapter 13.1
Chapter 13.1Chapter 13.1
Chapter 13.1patcha535
 
Chapter 12.3 (homework)
Chapter 12.3 (homework)Chapter 12.3 (homework)
Chapter 12.3 (homework)patcha535
 
Chapter 12.3
Chapter 12.3Chapter 12.3
Chapter 12.3patcha535
 
Chapter 12.2
Chapter 12.2Chapter 12.2
Chapter 12.2patcha535
 
Chapter 12.1.2
Chapter 12.1.2Chapter 12.1.2
Chapter 12.1.2patcha535
 
Chapter 12.1.1
Chapter 12.1.1Chapter 12.1.1
Chapter 12.1.1patcha535
 

More from patcha535 (20)

Chapter 14.2
Chapter 14.2Chapter 14.2
Chapter 14.2
 
Chapter 14.1
Chapter 14.1Chapter 14.1
Chapter 14.1
 
Chapter 13.2 (homework)
Chapter 13.2 (homework)Chapter 13.2 (homework)
Chapter 13.2 (homework)
 
Chapter 13.2
Chapter 13.2Chapter 13.2
Chapter 13.2
 
Chapter 13.1.10
Chapter 13.1.10Chapter 13.1.10
Chapter 13.1.10
 
Chapter 13.1.9
Chapter 13.1.9Chapter 13.1.9
Chapter 13.1.9
 
Chapter 13.1.8
Chapter 13.1.8Chapter 13.1.8
Chapter 13.1.8
 
Chapter 13.1.7
Chapter 13.1.7Chapter 13.1.7
Chapter 13.1.7
 
Chapter 13.1.6
Chapter 13.1.6Chapter 13.1.6
Chapter 13.1.6
 
Chapter 13.1.5
Chapter 13.1.5Chapter 13.1.5
Chapter 13.1.5
 
Chapter 13.1.4
Chapter 13.1.4Chapter 13.1.4
Chapter 13.1.4
 
Chapter 13.1.3
Chapter 13.1.3Chapter 13.1.3
Chapter 13.1.3
 
Chapter 13.1.2
Chapter 13.1.2Chapter 13.1.2
Chapter 13.1.2
 
Chapter 13.1.1
Chapter 13.1.1Chapter 13.1.1
Chapter 13.1.1
 
Chapter 13.1
Chapter 13.1Chapter 13.1
Chapter 13.1
 
Chapter 12.3 (homework)
Chapter 12.3 (homework)Chapter 12.3 (homework)
Chapter 12.3 (homework)
 
Chapter 12.3
Chapter 12.3Chapter 12.3
Chapter 12.3
 
Chapter 12.2
Chapter 12.2Chapter 12.2
Chapter 12.2
 
Chapter 12.1.2
Chapter 12.1.2Chapter 12.1.2
Chapter 12.1.2
 
Chapter 12.1.1
Chapter 12.1.1Chapter 12.1.1
Chapter 12.1.1
 

Recently uploaded

Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Jeffrey Haguewood
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
OnBoard
 
PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)
Ralf Eggert
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
Thijs Feryn
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
Sri Ambati
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
DanBrown980551
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
Cheryl Hung
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Product School
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
Product School
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
RTTS
 
Search and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical FuturesSearch and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical Futures
Bhaskar Mitra
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
Jemma Hussein Allen
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
Product School
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
Product School
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
91mobiles
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Ramesh Iyer
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Thierry Lestable
 

Recently uploaded (20)

Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
 
PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
 
Search and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical FuturesSearch and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical Futures
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
 

Chapter 13.1.11

  • 1. 20 C PREPROCESSOR 20.1 INTRODUCTION We have already seen many features provided by C language. Yet another unique feature of the C language is the preprocessor. The C preprocessor provides several tools that are not available in other high–level languages. The programmer can use these tools to make his program more efficient in all respect. 20.2 OBJECTIVES After going through this lesson you will be able to explain preprocessor working explain the # define Directive define constants explain macros write the various directions such as # undef, #include, #fdef, #ifdef, #ifndef, # else, #if 20.3 HOW THE PREPROCESSOR WORKS When you issue the command to compile a C program, the program is run automatically through the preprocessor. The preprocessor is
  • 2. 310 :: Computer Applications a program that modifies the C source program according to directives supplied in the program. An original source program usually is stored in a file. The preprocessor does not modify this program file, but creates a new file that contains the processed version of the program. This new file is then submitted to the compiler. Some compilers enable the programmer to run only the preprocessor on the source program and to view the results of the preprocessor stage. All preprocessor directives begin with the number or sharp sign (#). They must start in the first column, and no space is required between the number sign and the directive. The directive is terminated not by a semicolon, but by the end of the line on which it appears. The C preprocessor is a collection of special statements called direc- tives, that are executed at the beginning of the compilation process. The #include and # define statements are preprocessor directives. 20.4 THE # DEFINE DIRECTIVE The #define directive is used to define a symbol to the preprocessor and assign it a value. The symbol is meaningful to the preprocessor only in the lines of code following the definition. For example, if the directive #define NULL 0 is included in the program, then in all lines following the definition, the symbol NULL is replaced by the symbol. If the symbol NULL is written in the program before the definition is encountered, however, it is not replaced. The # define directive is followed by one or more spaces or tabs and the symbol to be defined. The syntax of a preprocessor symbol is the same as that for a C variable or function name. It cannot be a C keyword or a variable name used in the program; if it is so a syntax error is detected by the compiler. For example, suppose a program contains the directive #define dumb 52 which in turn is followed by the declaration int dumb; This would be translated by the preprocessor into int 52; which would be rejected by the compiler. The preprocessor does not check for normal C syntax, except for identifying quotation marks. It merely substitutes symbols where it finds them. The sym-
  • 3. C Preprocessor :: 311 bol being defined is followed by one or more spaces or tabs and a value for the symbol. The value can be omitted, in which case the symbol is defined but not given a value. If this symbol is used later in the program, it is deleted without being replaced with anything. If a # define directive does not fit on a single line, it can be contin- ued on subsequent lines. All lines of the directives except the last must end with a backslash( ) character. A directive can be split only at a point where a space is legal. 20.5 CONSTANTS A common use for defined symbols is the implementation of named constants. The following are examples of constants in C: 23, ‘a’, “hello” Any of these can be assigned to a defined preprocessor symbol: #define INTEGER 23 #define CHARACTER ‘a’ A defined symbol can specify only a complete constant. For example, if a program contains the definition #define NULL 0 the number 120 cannot be represented by 12 NULL. A defined sym- bol can be recognized only if it is delimited by white space, punc- tuation or operators. 20.6 MACROS When a constant is associated with a defined symbol, the characters making up the constants are assigned , not the numeric value of the constant. The definition #define EOF-1 really assigns the string “-1” to EOF. The preprocessor replaces the symbol EOF by the characters “-1” without any consideration of the meaning of the two characters. When a preprocessor symbol is defined as a segment of text, it is more generally called a macro. Macros are very useful in making C code more readable and compact. For example, some programmers include the following definitions in all their programs:
  • 4. 312 :: Computer Applications # define and && # define or || These definitions enable the programmer to write more readable code, such as the following: If (a<b or c>d and e<f) A comment can be included at the end of a macro or constant defi- nition: # define same 1 /* Nothing */ A comment can be inserted at any point in a C program where white space is permitted without adverse effect to the program. Macros also can be used as abbreviation for lengthy and frequently used statements. The one restriction on macros is that, even though their definitions can occupy more than one line, they cannot include a newline. That is, when a macro is expanded all the resulting text is placed on the same line. The preprocessor permits macros to have arguments, just as func- tions do; the difference is that the arguments are strings rather than values. Consider the following definition. #define Decrement(x) if (x>0)x - = 1 when this macro is expanded, the string passed to the argument x replaces all occurrences of x in the symbol’s value. That is, the state- ment Decrement(a); is expanded to if(a>0) a- = 1; and the statement Decrement(b); becomes if (b>0) b - =1; Notice that the macro definition itself does not include a semicolon. When the macro is invoked later, it is followed by an explicitly speci- fied semicolon:
  • 5. C Preprocessor :: 313 Decrement(a); The argument names used in a macro are local to the macro. Thus there is no conflict between a macro’s formal parameter names and identifiers used in the program itself. Any symbols used in the macro definition which are not argument name or names of other macros are left unchanged, and are assumed to be variable names or other C symbols. The argument supplied to a macro can be any series of characters. The comma is the delimiter that separates arguments in a macro. A macro can be defined in terms of another macro, as in the follow- ing example of nested macros: #defined control “%dn” #define printint(x) printf(control,x) #define test(x) if (x>0) printint(x) If a program contains the statements Test(w); Where w is an integer variable, the statement goes through the following conversion steps: i) if(w>0) printint(w); ii) if (w>0) printf(CONTROL,w); iii) if(w>0) printf (“%dn,”w); INTEXT QUESTIONS 1. What, in general terms, is the role played by the C preproces- sor ? 2. Which symbol always precedes preprocessor directives ? 3. Where on the line must all preprocessor directives begin ? 4. Where can a preprocessor directive be written ? 5. Which symbol is used to separate the arguments of a macro ?
  • 6. 314 :: Computer Applications 20.7 USE OF DIRECTIVES: # undef, # include (a) The # undef Directive:- If a preprocessor symbol has already been defined, it must be unde- fined before being redefined. This is accomplished by the #undef directive, which specifies the name of the symbol to be undefined. It is not necessary to perform the readefinition at the beginning of a program. A symbol can be redefined in the middle of a program so that it has one value in the first part and another value at the end. A symbol need not be redefined after it is undefined. (b) The #include Directive:- Often a programmer accumulates a collection of useful constants and macro definitions that are used in almost every program. It is desirable to be able to store these definitions in a file that can be inserted automatically into every program. This task is performed by the #include directive. A file is inserted at a point where the #include directive is encoun- tered. Such files are called header files, and by convention their names end with the character.h (as in stdio.h). 20.8 CONDITIONAL COMPILATION: #ifdef, #ifndef and #endif Removing statements by hand would be quite tedious and could also lead to error. For this reason, the preprocessor provides directives for selectively removing section of code. This process is known as conditional compilation. #define RECORD–FILE If the # ifdef directive tests whether a particular symbol has been defined before the #ifdef is encountered. It does not matter what value has been assigned to the symbol. In fact, a symbol can be defined to the preprocessor without a value. If the #ifdef directive is encountered after this definition it produce a true result. If, however, the directive #undef RECORD–FILE is encountered before the directive #ifdef RECORD–FILE then the preprocessor considers the symbol RECORD–FILE to be undefined and the #ifdef directive returns a false value.
  • 7. C Preprocessor :: 315 If an #ifdef returns a true value, all the lines between the #ifdef and the corresponding #endif directive are left in the program. If those lines contains preprocessor directives, the directives are processed. In this way, conditional compilation directives can be nested. If the #ifdef evaluates as false, the associated lines are ignored, including any preprocessor directives that are included. The statements need not all be grouped in one place. The #ifdef and #fendif directives can be used as many times as required. The pre- processor also provides the directive #ifndef, which produces a true result if a symbol is not defined. This makes it possible to use a single symbol to switch between two versions. Conditional compilation can be used to select preprocessor direc- tives as well as C code. For example suppose a header file is included in a program. and a certain preprocessor symbol (say, FLAG) may or may not be defined in that header file. If the programmer wants FLAG never to be defined, then the #include directive can be followed by #ifdef FLAG #undef FLAG #endif This ensures that, even if the symbol is defined in the header file, its definition is removed. It is not sufficient merely to work on #undef FLAG because if FLAG is not defined, the directive is erroneous. If FLAG should always be defined, then we would write #ifndef FLAG #define FLAG #endif We could, of course, give FLAG a value. We cannot simply write #define FLAG since if FLAG is already defined, an error probably will result. 20.9 THE #ELSE DIRECTIVE This directive functions in much the same way as the else clause of an if statement.
  • 8. 316 :: Computer Applications All lines between an #ifdef or an #ifndef directive and the corre- sponding # else clause are included if the #ifdef or #ifndef is true. Otherwise, the lines between the #else and the corresponding #endif are included. The # else directive and the lines that follow it can be omitted, but never the #endif directive. No other text can be included on the same line as the #else or #endif directive. 20.10 THE # IF DIRECTIVE The #if directive tests an expression. This expression can be of any form used in a C program, with virtually any operators, except that it can include only integer constant values No variables, or function calls are permitted, nor are floating point, character or string con- stants. The #if directive is true, if the expression evaluates to true (non– zero). Any undefined preprocessor symbol used in the #if expression is treated as if it has the value φ. Using a symbol that is defined with no value does not work with all preprocessors, and an attempt to do so might result in an error. INTEXT QUESTIONS 6. What is the characteristics of the #else and the #endif directives? 7. How is the #ifndef directive used? 8. State the reason for using the #ifdef directive. 20.11 WHAT YOU HAVE LEARNT In this lesson, you have learnt about preprocessors. The preprocessor is a program which modifies the C source program according to instructions provided. Remember that all preprocessor directives begin with the symbol #. There are different preprocessor directives. You also learnt about Macros. This lesson also discussed about conditional compilation. 20.12 TERMINAL QUESTIONS 1. How can a #define directive be continued to a new line ? 2. Where can a preprocessor directive be split ?
  • 9. C Preprocessor :: 317 3. What is a macro, and what is it used for ? 4. What can a macro argument consist of ? 5. What role is played by the #undef directive? 6. How is the # include directive used ? 7. What is conditional compilation ? 8. How does the #if directive operate ? 20.13 KEY TO INTEXT QUESTIONS 1. It provides for the implementation of macros and conditional compilation. 2. The # sign. 3. The first column. 4. Anywhere in a program, so long as it starts in the first column of a line and is not on the same line as another directive or C statement. 5. The comma. 6. They only allow that text can be included on the lines they occupy. 7. It passes lines of code to the compiler only if the specificed preprocessor symbol is not defined. 8. The #ifdef directive is used in order to pass certain lines of code of the compiter, only if the specified preprocessor symbol is defined before the #ifdef directive is encounted.