Full Stack Developer
The #else preprocessor directive evaluates the expression or condition if the condition of #if is false. It can be used with #if, #elif, #ifdef and #ifndef directives. Differently is a directive in C programming language that helps to give the statements those need to be executed when the conditions are given using#if,#ifdef or#ifndef directives evaluates to false. Once the condition given in these directives evaluates to false,#else directives provide alternate statements to be executed. It's a part of the preprocessor directive since it's called by the compiler automatically before factual compendium thresholds. Before a C program is collected by the compiler source law is reused therefore this process is called preprocessing. All the commands used for the preprocessor are known as preprocessor directives and all preprocessor directives are defined using#.
#if _condition_
// Statements to be executed when condition returns TRUE
#else
// statements to be executed when condition in #if results to false.
#endif
Preprocessors refer to the programs that are processed in our source code even before code enters the compiler for compilation. # undef is such a command for the preprocessor.
Various preprocessor directives can be defined which can be categorized into below 4 main categories.
There are 4 main types of preprocessor directives:
The source code written by the user is first sent for preprocessing to the preprocessors which generate an expanded source file with the same name as that of the program. This expanded file is further sent for the compilation to the compiler to generate an object code of the library functions and once this object code is linked to the various library functions being used, an executable ( .exe) file is generated.
#else directive is used to provide an alternate statement that needs to be executed when the condition is given using #if, #ifdef, or #ifndef. Whenever the condition returns false compiler sends the control directly to the #else block statement.
Certain rules need to be followed for declaring conditional expression:
After the #if or #elif directives #else blocks come into action. All the #if.. #elif.. #else block must be ended using #endif directive that tells the compiler that if-else block is over.
/* Example using #else directive by TechOnTheNet.com */
#include <stdio.h>
#define YEARS_OLD 12
int main()
{
#if YEARS_OLD < 10
printf("TechOnTheNet is a great resource.\n");
#else
printf("TechOnTheNet is over %d years old.\n", YEARS_OLD);
#endif
return 0;
}
Output:
TechOnTheNet is over 12 years old.