# elif in c


Processor directive #elif is part of #if directive. It will execute only if it’s just previous constant expression of #if or #elif is zero.  

Syntax:

#if <Constant_expression>
    ------------------
    ------------------
#elif <Constant_expression>
    ------------------
    ------------------
………………………….
………………………..
#elif <Constant_expression>
    ------------------
    ------------------

#else
    ------------------
    ------------------
#endif

Example 1:

#include<stdio.h>
int main(){
    int num=11;
    #if(num>0)
         ++num;
    #elif(num==0)
         --num;
    #else
         num=0;
    #endif
    printf("%d",num);
return 0;
}


Output: Compilation error
Explanation: we cannot use logical operator > with undefined macro constant num.

Note: Preprocessor directive #if will treat num as macro constant not as integer variable since it execute just before the actual c code.

Example 2:

#include<stdio.h>
int main(){
    #if(!5>=5)
         int a=5;
    #elif -1
         int a=10;
    #else
         int a=15;
    #endif
    printf("%d",a);
return 0;
}


Output: 10
Explanation: Consider on the expression:  !5 >= 5
= 0 >= 5
= 0
So #if is false. Now it will execute first #ielif directive. Since -1 is non-zero number. So condition is true so #else directive will not execute.

Its intermediate file will look like:

int main(){
    int a=10;
    printf("%d",a);
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.