#ifdef and #endif in c



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.








  • Preprocessor definitions in c
  • Preprocessor directive in c
  • #include directive in c
  • # define directive in c
  • Pragma directive in c
  • Warning directive
  • Preprocessor operators in c
  • # if directive in c
  • #line directive in c
  • # error directive in c
  • # elif in c
  • # ifdef and #endif in c
  •  # ifndef in c example
  • #undef in c
  • What is header file in ?
  • C preprocessor questions
  • C tutorial home.
  • 1 comment:

    Unknown said...

    Thanks but what about #endif?