In this video tutorial lets learn about preprocessor command or directives like #ifndef, #else and #endif. These directives are used for conditional compilation. #ifndef works completely opposite to #ifdef
The block of code between #ifndef and #endif works only if the macro name is NOT defined. If the macro name is defined, the compiler will skip the entire block of code inside #ifndef from compiling.
#include<stdio.h>
#define iOS
int main()
{
#ifndef iOS
printf("I Love Apple Devices\n");
#else
printf("Code for Non Apple Devices\n");
#endif // iOS
return 0;
}
#include<stdio.h>
int main()
{
#ifndef iOS
printf("I Love Apple Devices\n");
#else
printf("Code for Non Apple Devices\n");
#endif // iOS
return 0;
}
Output: I Love Apple Devices
In above programs, if iOS macro name is defined, then the set of code present inside #else block gets compiled and executed. If macro name iOS is not defined, then the code inside #ifndef block gets compiled and executed.
Note:
1. Using #else is optional. You can just use #ifndef and #endif.
2. Working of #ifndef is opposite to that of #ifdef.
3. This is similar to if else condition, but here the block of code which do not match the criteria doesn’t even get compiled. It’s treated like regular comments.
In this video tutorial lets learn about preprocessor command or directives like #if, #elif, #else and #endif. These directives are used in conditional compilation.
The block of code inside #if block works only if the condition or the expression in resolved to non-zero number. If the expression is resolved to zero, then the code inside #if is skipped from compilation.
Same is true for #elif: If the condition or the expression is resolved to 0 then the code inside #elif block is skipped from compilation. If it’s resolved to non-zero number then the block of code inside #elif gets compiled and executed.
#else is optional and when used, it should be present only once between #if and #endif, and it should be present at the end(just before the #endif directive). If none of the conditions in #if and #elif match, then the code inside #else directive block gets executed.
#elif is also optional. But it can be used any number of times inside #if and #endif. But it should always be written after #if and not before it. It should be present before #else and not after #else directive.
Note: We can have nested #if #elif #else #endif inside #if and/or #elif and/or #else block. Nesting can go any number or depth. But remember to have #endif for every #if.
Video Tutorial: Conditional Compilation In C: #if #elif #else #endif
1. When you want certain code to be skipped: #if or #elif or #else part of the code is compiled and executed only when the conditions are met.
2. To write portable code: Again, like that of in #ifdef and #else, we could write portable code. That is, we can write code for specific devices in #if, #elif and #else part and the compiler compiles the code specific to that device. All other code will be treated as comments.
3. Saving memory and file size: We could use it in big projects. We could include device specific files inside #if, #elif and #else blocks depending upon the condition. This way we could avoid including or importing all the files into our source code.
#include<stdio.h>
#define MARKS 50
int main()
{
#if(MARKS <= 100 && MARKS >= 80)
printf("GRADE A\n");
#endif // MARKS
printf("Your Result\n");
return 0;
}
Output: GRADE A Your Result
Check the condition for #if. We can use logical operator and/or arithmetic operators and/or relational operators and/or Conditional Operators in the condition.
#include<stdio.h>
#define MARKS 50
int main()
{
#if(MARKS <= 100 && MARKS >= 80)
printf("GRADE A\n");
#elif(MARKS <= 79 && MARKS >= 60)
printf("GRADE B\n");
#elif(MARKS <= 59 && MARKS >= 40)
printf("GRADE C\n");
#elif(MARKS <= 39 && MARKS >= 30)
printf("GRADE D\n");
#else
printf("Please retake the test!\n");
#endif // MARKS
printf("Your Result\n");
return 0;
}
Output: GRADE C Your Result
Only 1 block of code is compiled and executed here, even if multiple conditions are satisfying. Whichever condition returns non-zero number first, the code inside its block gets executed. All other code will be skipped.
In this video tutorial lets learn about preprocessor command or directives like #ifdef, #else and #endif. These directives are used for conditional compilation.
How Does #ifdef Work?
The block of code between #ifdef and #endif works only if the macro name is defined orelse the compiler will skip the entire block of code from compiling.
When To Use #ifdef
1. When we want the compiler to skip certain part of the source code: For this we could even use multi-line comment, but if we already had multi-line comments in the code, we can’t enclose the source code and multi-line comments with another multi-line comment. Nesting of multi-line comments are not allowed in C programming language. So instead of commenting the code we could make use of #ifdef directive.
2. To write portable code: For example, we could write code both for iOS and non-iOS devices. Using #ifdef we could check the device OS and based on that deliver specific set of codes. For this we can use #ifdef
Video Tutorial: Conditional Compilation In C: #ifdef #else #endif
#include<stdio.h>
int main()
{
#ifdef iOS
printf("This is iOS Code\n");
#else
printf("This is code for Android Devices\n");
#endif // iOS
return 0;
}
Output: This is code for Android Devices
In above programs, if iOS macro name is defined, then the set of code present inside #ifdef block gets compiled and executed. If macro name iOS is not defined, then the code inside #else block gets compiled and executed.
#include<stdio.h>
#define iOS
int main()
{
#ifdef iOS
printf("This is iOS Code\n");
#endif // iOS
printf("This is code for all non iOS Devices\n");
return 0;
}
Output: This is iOS Code This is code for all non iOS Devices
#include<stdio.h>
int main()
{
#ifdef iOS
printf("This is iOS Code\n");
#endif // iOS
printf("This is code for all non iOS Devices\n");
return 0;
}
Output: This is code for all non iOS Devices
Note: using #else is optional. You can just use #ifdef and #endif.
This is similar to if else condition, but here the block of code which do not match the criteria doesn’t even get compiled. It’s treated like regular comments.
If you’re a computer science student enrolled in University, your syllabus may not include in-depth learning of preprocessors. But when it comes to competitive exams and real-time application programming you’ll have to use preprocessors a lot. So better learn it now.
In upcoming videos we’ll cover preprocessor concepts in detail with simple example programs to explain the concepts/topics.
So stay tuned, stay subscribed to our YouTube channel and blog.