#ifndef statement

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

#ifndef statement : Conditional Compilation Directives (C Preprocessor)

What it does ?

  • These Conditional Compilation Directives allow us to include certain portion of the code depending upon the output of constant expression
  • Block is Called as Conditional Group

Syntax :

#ifndef MACRONAME 

Explanation :

  1. If the MACRONAME specified after #ifndef is not defined previously in #define then statement_block is followed otherwise it is skipped
  2. We say that the conditional succeeds if MACRO is not defined, fails if it is not.

Live Example 1 :


void main()
// Define another macro if MACRO NUM is defined 

#ifndef NUM
      #define MAX 20 

printf("MAX number is : %d",MAX);

Output :

MAX Number is 20

Live Example 2 :

#define MAX 90

void main()
#ifndef MAX
      #define MIN 90
      #define MIN 100

printf("MIN number : %d",MIN);


Output :

MIN number : 100

Rules :

  1. The MACRONAME inside of a conditional can include preprocessing directives.
  2. They are executed only if the conditional succeeds.
  3. You can nest conditional groups inside other conditional groups, but they must be completely nested.
  4. You cannot start a conditional group in one file and end it in another.