Home »
C programs »
C preprocessors programs
C Preprocessor Directive - #if … #else
By IncludeHelp Last updated : March 10, 2024
C - #if #else directive
The #if ... #else is a preprocessor directive in C programming language and it is used for conditional compilation, where one of the code section needs to be compiled based on the condition from two given code sections.
Syntax
General syntax for of the #if ... #else directive is:
#if conditional_expression
statements-true;
#else
statements-false;
#endif
If conditional_expression is true, the code written between in #if block i.e. statements-true will be executed and if conditional_expression is false, code written in #else block i.e. statements-false will be executed.
#if … #else Directive Example
The following is an example of #if … #else directive:
#include <stdio.h>
#define MAX_GF 20
int main(){
printf("Hello\n");
#if MAX_GF>=50
printf("Wau, you may have more than 50 girlfriends\n");
printf("Code for more than 50 GFs\n");
#else
printf("Wau, you may not have more than 50 girlfriends\n");
printf("Code for less than 50 GFs\n");
#endif
printf("Bye!\n");
return 0;
}
Output
Hello
Wau, you may not have more than 50 girlfriends
Code for less than 50 GFs
Bye!
C Preprocessors Programs »