Pages

PREPROCESSING UNIT 8 PART 1



Introduction
C's preprocessor provides a facility for defining constant and substitution, which are commonly called macros. It is used when you either want the make to program more readable or when you don't have enough information about certain values. For example, if your input is in U.S. dollars, and your processing is done in terms of rupees, then your program may have the expression
Rs = usd * 46;
where 46 is the currency rate. You can write the expression as:
# define currency_rate 46
rs = usd * currency_rate;
So, if the currency rate is changed, you can make the necessary change only in one place. The preprocessor directive is defined here.
Program
# include <stdio.h>

#define VAL 35        // A
#define HELLO "HELLO";      // B

main ()
{
    int res;

    res = VAL-5;            // C
    printf ("res = VAL-5: res == %d\n", res);
    printf ( HELLO);        //D
}
Statements A and B indicate preprocessor directives. VAL is defined as integer 35 and HELLO is a string as "HELLO". Whenever VAL and HELLO appear, they are replaced by the specified values.
In statement C, VAL 35 is replaced by VAL −5. So the statement becomes
res = 35-5;
Statement D, after replacement, becomes
printf ("HELLO")
The preprocessor directives are not C statements, so they do not end with semicolons.
The include directive tells the compiler to include all the contents of a specified file in the source file before giving the source file for compiling.
Explanation
  1. The preprocessor substitutes strings that are specified by using define directive
  2. #define constant identifer "value"
  3. Following are valid define expressions:
  4. #define TRUE           1
  5. #define FALSE          0
  6. #define BS      '\b'
  7. #define TAB            '\011'

undef
Introduction
If you want to nullify the effect of the define directive and specify a new value, then you can use the undef directive.
Program

#include <stdio.h>
#define VAL 40;      //A
#undef VAL           //B
#define VAL 40       //C
main()
{
    printf ("%d\n", VAL);   //D
}
Explanation
  1. Statement A defines VAL as 40, that is, an erroneous definition.
  2. Statement B indicates that the afore mentioned definition no longer exists.
  3. Statement C allows a new definition.
  4. Statement D uses new definition of 40.
Point to Remember
The undef directive nullifies the effect of an earlier definition.
ifdef
Introduction
The ifdef directive makes substitutions based on whether a particular identifier is defined.
Program
Suppose we have three files:
file1.h
#define USD 1
file2.h
#define UKP 1
file3
#include <stdio.h>
#include <file1.h>       //A
#ifdef USD                  // B
       #define currency_rate 46     //C
#endif                //D

#ifdef UKP                  // E
   #define currency_rate 100 //F
#endif                //G
main()
{
    int rs;
    rs = 10 * currency_rate;  //H
    printf ("%d\n", rs);
}
Explanation
  1. Statement A includes file1.h, so the content of the file is substituted in that position. If the file name is given in angle brackets, it means the file is searched in the default search path. If the file name is specified within “”, like include "file1.h", then file1.h is searched only in the current directory.
  2. Statement B is an ifdef directive and it checks whether the identifier USD is defined. Since it is defined in file1.h, the condition is true and the currency rate is defined as 46. You can include multiple directives in if, def and endif.
  3. Statement E checks whether the identifier UKP is defined. Since it is not defined, because file2.h, is not included in the file, the condition is false and its defined directive is not processed.
  4. The currency rate in file3 is taken as 46.
  5. In the expression in statement H, the currency rate is substituted as 46.
  6. If, instead of file1.h, you include file2.h, then the currency rate will be 100.
Points to Remember
  1. ifdef is used to make a substitution depending on whether a certain identifier is defined.
  2. If the identifier is defined, it returns true; otherwise, it is false
ifndef
Introduction
#ifndef is used to check whether a particular symbol is defined.
Program
Suppose wse have three files:
file1.h
#define USD 1

file2.h
#define UKP 1

file3
#include <stdio.h>
#include <file1.h>      //A

#ifndef USD                   // B
       #define currency_rate 100     //C
#endif                //D

#ifndef UKP                  // E
   #define currency_rate 46 //F
#endif                //G
main()
{
    int rs;
    rs = 10 * currency_rate;  //H
    printf ("%d\n", rs);
}
Explanation
  1. ifndef is a complement of ifdef. That is, if the symbol is defined, ifndef returns false.
  2. Statement B is an ifndef directive and it checks whether the identifier USD is defined. Since it is defined in file1.h, the condition is false and further processing is not done.
  3. Statement E checks whether the identifier UKP is defined. Since it is not defined, because file2.h is not included in the file, the condition is true and the currency rate is defined as 46.
  4. The currency rate in file3 is taken as 46.
  5. In the expression in statement H, the currency rate is substituted as 46.
  6. If, instead of file1, you include file2.h, then the currency rate will be 100.
  7. ifndef is a complement of ifdef. So, when ifdef returns true, ifndef returns false.