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 (
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
- The preprocessor substitutes
strings that are specified by using define directive
- #define constant identifer
"value"
- Following are valid define expressions:
- #define TRUE 1
- #define FALSE 0
- #define BS '\b'
- #define TAB '\011'
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
- Statement A defines VAL as 40, that is, an
erroneous definition.
- Statement B indicates that
the afore mentioned definition no longer exists.
- Statement C allows a new
definition.
- Statement D uses new
definition of 40.
Point to Remember
The undef directive nullifies the effect of an earlier
definition.
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>
#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
- 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.
- 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.
- 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.
- The currency rate in file3 is taken as 46.
- In the expression in
statement H, the currency rate is substituted as 46.
- If, instead of file1.h, you include file2.h, then the currency rate
will be 100.
Points to Remember
- ifdef is used to make a
substitution depending on whether a certain identifier is defined.
- If the identifier is
defined, it returns true; otherwise, it is false
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
- ifndef is a complement of ifdef. That is, if the symbol is
defined, ifndef returns false.
- 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.
- 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.
- The currency rate in file3 is taken as 46.
- In the expression in
statement H, the currency rate is substituted as 46.
- If, instead of file1, you include file2.h, then the currency rate
will be 100.
- ifndef is a complement of ifdef. So, when ifdef returns true, ifndef returns false.