Directive #ifdef is very similar to #if except its conditional statement which is identifier instead of a constant expression. Identifier may a macro constant or global identifier. It only checks identifier has been defied or not. It doesn’t care what the value of identifier is. If identifier has been defined then it executes #ifdef body otherwise it executes the body of #else directive.
Note: Global identifiers are predefined macro constats.
Syntax:
#ifdef <Identifer>
-------------
-------------
#else
-------------
-------------
#endif
Example 1:
#include<stdio.h>
#define ABC 25
#define PQR "Exact Help"
int main(){
int num = 3;
#ifdef ABC
printf("%d",ABC * ABC);
#else
printf("%s",PQR);
#endif
return 0;
}
Output: 625
Explanatiopn: Since macro constant ABC has defined so #ifdef condition is true.
#include<stdio.h>
int main(){
#ifdef __DATE__
printf("%s",__DATE__);
#else
printf("First define the __DATE__");
#endif
return 0;
}
Output: It will print current system date.
Explanation: __DATE__ is global identifier. It has already defined in the header file stdio.h and it keeps the current system date.
1 comment:
Thanks but what about #endif?
Post a Comment