#ifndef in c example



Directive #ifndef is just opposite to the directive #ifdef . In this case if identifier has not defined then #ifndef is true and if identifier has defined then #ifndef condition will false.

Syntax :

#ifndef <Identifier>
    --------------
    --------------
#else
    --------------
    --------------
#endif

Example 1:

#include<stdio.h>
#define int ‘A’

int main(){
    char num = int;
    #ifndef int
         printf("Please define int");
    #else
         printf("%d",int);
    #endif

return 0;
}

Output: 65
Explanation: Macro constant int has been defined so #else directive will execute. Its intermediate file will look like:

int main(){
    char num = ‘A’;
    printf("%d",’A’);
return 0;
}

ASCII value of ‘A’ is 65.


Example 2:
#include<stdio.h>
int main(){
        
#ifndef __TIME__
         Wow! We can write any thing. Exact help.
         < * 9 j x %% && ## ++ (( ))
    #else
         printf("%s",__TIME__);
    #endif

return 0;
}

Output: It will print current system time.
Explanation:  __TIME__ is global identifier. It has been defined in the header file stdio.h. Compiler doesn’t compile the c codes which are inside the any conditional preprocessor directive if its condition is false. So we can write anything inside it.
Its intermediate file will look like:

int main(){

    printf("%s",__TIME__);

return 0;
}








  • 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.
  • Post a Comment