#if directive in c


There are total six conditional compilation directives. There are:
(a)#if
(b)#elif
(c)#else
(d)#endif
(e)ifdef
(f)ifndef


#if directive :
    It is conditional compilation directive. That is if condition is true then it will compile the c programming code otherwise it will not compile the c code.

Syntax 1:

#if <Constant_expression>
    -------------
    -------------
#endif


If constant expression will return 0 then condition will ture if it will return any non zero number condition will true.

Example:

#include<stdio.h>
#if 0
int main(){
    printf("HELLO WORLD");
return 0;
}
#endif

Output: Run time error, undefined symbol _main


Explanation: Due to zero as a constant expression in #if condition will false. So c code inside the #if condition will not execute. As we know without any main function we cannot execute any code.

We can also write #else in the #if directive. #else directive will only execute if condition is false.

Syntax 2:

#if <Constant_expression>
    -----------------
    -----------------
#else
    -----------------
    -----------------
#endif

Example:

#include<stdio.h>
#if(10)
int main(){
    printf("errorandexception.blogspot.com");
return 0;
}
#else
int main(){
    We can write any thing
    5= a int;
    if(for(while(1);;));
    ++5;
    10=24;
return 0;
}
#endif

Output: errorandexception.blogspot.com
Explanation: 10 is non zero integer constant. So #if condition is true.

Example:

#include<stdio.h>
#if -2
int main(){
    printf("HELLO WORLD");
return 0;
}
#else
int main(){
    printf("errorandexception.blogspot.com");
return 0;
}
#endif

Output: HELLO WORLD

Explanation: -2 is non zero number so #if condition is true.

Note: Consonant expression in #if condition should not include any c programming variable since all preprocessor directives execute just before the actual c code.

For example:

#include<stdio.h>
int main(){
int var = 5;
    #if var
        printf("errorandexception.blogspot.com");
    #else
         printf("cquestionbank.blogspot.com");
    #endif

return 0;
}

Output: cquestionbank.blogspot.com
Explanation: Directive #if will not think expression constant var as integer variable and also it will not throw an error. Then what is var for directive #if?

Directive #if will treat var as underfed macro constant. In c any underfed macro constant return zero so #else directive will execute. So proper way of above c code is:

#include<stdio.h>
#define var 10

int main(){
    #if var
        printf("errorandexception.blogspot.com");
    #else
         printf("cquestionbank.blogspot.com");
    #endif

return 0;
}

Output: errorandexception.blogspot.com

Explanation: Macro constant var will be replaced 10. Since 10 is non-zero number so #if part will execute.

Note: Constant expression in #if directive cannot be string constant. It can be character constant which returns its ASCII value to directive.







  • 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.
  • 2 comments:

    bharath said...

    thanks ..this site solved many of my doubts

    Unknown said...

    very good explanation with usage in example