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#include //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.
No comments:
Post a Comment