What are #if, #elif, and #else in C
The #if, #elif, and #else directives in C are part of the preprocessor. They help you control which parts of your code get compiled based on certain conditions. These directives are not regular C code – they are processed before compilation begins.
Purpose of #if, #elif, and #else:
- When you want to compile different code for different platforms or operating systems.
- You are testing different features or debug options.
- You want to include or exclude certain code blocks without deleting them.
#if Directive in C
The #if directive checks if a given constant expression evaluates to true (non-zero). If so, the following code is included at compile-time.
Syntax:
#if constant_expression // Code to compile if condition is true #endif
Example:
#include <stdio.h> #define QUIZ_LEVEL 1 int main() { #if QUIZ_LEVEL == 1 printf("Beginner level quiz selected.\n"); #endif return 0; }
This C program uses a macro QUIZ_LEVEL set to 1. Inside the main function, a #if directive checks if QUIZ_LEVEL equals 1. Since it does, it prints “Beginner level quiz selected”. This shows how macros can control which code runs based on conditions at compile time.
#elif Directive in C
The #elif directive stands for “else if”. It provides another condition if the #if fails.
Syntax:
#if condition1 // Code for condition1 #elif condition2 // Code for condition2 #endif
Example:
#include <stdio.h> #define QUIZ_LEVEL 2 int main() { #if QUIZ_LEVEL == 1 printf("Beginner level selected.\n"); #elif QUIZ_LEVEL == 2 printf("Intermediate level selected.\n"); #endif return 0; }
This C program defines a macro QUIZ_LEVEL as 2. In the main function, it uses #if and #elif to check the value of QUIZ_LEVEL. Since the value is 2, it prints “Intermediate level selected.” This shows how conditional compilation can select different code blocks based on macro values.
#else Directive in C
The #else directive runs when none of the previous #if or #elif conditions were true.
Syntax:
#if condition // Code for condition #else // Fallback code #endif
Example:
#include <stdio.h> #define QUIZ_LEVEL 3 int main() { #if QUIZ_LEVEL == 1 printf("Beginner quiz mode\n"); #else printf("Advanced quiz mode\n"); #endif return 0; }
This C program sets the macro QUIZ_LEVEL to 3. It uses #if to check if the level is 1; since it’s not, the #else block runs, printing “Advanced quiz mode”. This shows how #if and #else can control code flow based on macro values.
Combining #if, #elif, and #else
These directives can be combined to handle complex conditional compilation scenarios.
#include <stdio.h> #define QUIZ_LEVEL 2 int main() { #if QUIZ_LEVEL == 1 printf("Quiz Mode: Beginner\n"); #elif QUIZ_LEVEL == 2 printf("Quiz Mode: Intermediate\n"); #else printf("Quiz Mode: Advanced\n"); #endif return 0; }
Output:
Quiz Mode: Intermediate
This C program defines QUIZ_LEVEL as 2. It uses #if, #elif, and #else to choose which message to print. Since QUIZ_LEVEL is 2, it prints “Quiz Mode: Intermediate”. This shows how multiple conditions can control which code runs during compilation.
Sanfoundry Global Education & Learning Series – 1000 C Tutorials.
- Apply for Computer Science Internship
- Check C Books
- Practice BCA MCQs
- Watch Advanced C Programming Videos
- Check Computer Science Books