C Sharpelse Preprocessor Directive

It has the following syntax:

Example

#if expression  

//if code  

#else  

//else code  

#endif

Syntax with #elif:

C #elif processor has the following syntax:

Example

#if expression  

//if code  

#elif expression  

//elif code  

#else  

//else code  

#endif

Simple C #else Example

Let's explore a basic illustration demonstrating the utilization of the #else preprocessor directive in the C programming language.

Example

Example

#include <stdio.h>  

#include <conio.h>  

#define NUMBER 1  

void main() {    //main function

#if NUMBER==0  

printf("The value of a number is: %d",NUMBER);  

#else  

printf("The value of a number is non-zero");  

#endif         

getch();  

}

Output:

Output

The value of a number is non-zero

Explanation:

In the aforementioned instance, a macro has been set as 1 utilizing the #define preprocessor instruction. Subsequently, the main function has been established wherein a #if statement has been introduced with the condition "NUMBER == 0". Consequently, if this condition evaluates to true, the corresponding value will be displayed; otherwise, the program will proceed to the subsequent #else block, outputting the statement pertaining to that condition. The sequence is concluded with the #endif directive followed by the execution of the getch function. The resultant output reveals the display of the statement within the #else directive.

C Example to Check Conditions with #if and #else

Here, we will demonstrate an example to show how to evaluate conditions using #if and #else directives in the C programming language.

Example

Example

#include <stdio.h>  

#include <conio.h> 

#define NUMBER 6

void main( ) {   // main function  

#if NUMBER==6  

printf("The value of a number is even : %d ",NUMBER );  

#else  

printf("The value of a number is odd " );  

#endif         

getch( );  

}

Output:

Output

The value of a number is even : 6

Explanation:

In the aforementioned scenario, a macro was defined as 6 using the #define preprocessor directive. Subsequently, the main function was implemented, containing an #if directive with the condition "NUMBER == 6". Consequently, if the condition evaluates to true, it will display the value; otherwise, it will proceed to the subsequent '#else' directive and execute the corresponding statement. The control flow is concluded with the '#endif' directive, followed by the utilization of the getch function. Upon execution, the output demonstrates the printing of the '#if' directive statement.

C #else Example for Platform-Specific Compilation

Here, we will illustrate the #else directives for platform-specific compilation in the C programming language.

Example

Example

#include <stdio.h>



#define Logic PracticeT 1



int main() {   //main method

#if Logic PracticeT

    printf("The code compiled for Logic Practice\n");

#else

    printf("The code compiled for other Logic PracticeT\n");

#endif

}

Output:

Output

The code compiled for Logic Practice

Explanation:

In this instance, we employ a conditional compilation to ascertain the definition status of the macro Logic PracticeT with a true value. Subsequently, we have employed the #if directive to assess the macro, and if it evaluates to true, the code within the initial block is incorporated during compilation; otherwise, the program proceeds with the #else block. Given that Logic PracticeT is defined as 1, the code presents the message from the #if section.

Features of the C #else processor directives

There are several features of the C #else preprocessor directives. Some of them are as follows:

  • The C #else evaluates the next condition when the previous condition (#if or #elif) is false. It allows us to control the compilation process based on specific requirements.
  • This directive allows us to enable or disable the code sections before compilation, which helps to improve the efficiency of the program.
  • If we want to perform any operation with the #else directives in C, the condition of the #if, #ifdef, and #ifndef should be false.
  • We can use this #else directive inside some other conditional blocks that can provide more complex compile-time logic.
  • With the help of the C #else directive, we can compile different code for different environments or modes.
  • It helps to reduce the requirement for manual commenting at the time of development.
  • Conclusion

In summary, the #else directive within the C programming language offers a useful way to incorporate alternate code segments during compilation if the primary condition in #if, #ifdef, or #ifndef evaluates to false. This feature enhances flexibility, streamlines conditional compilation processes, and effectively handles platform-specific or debug-oriented code. By allowing fallback logic at compile time, the #else directive promotes the development of cleaner, easier-to-manage, and more flexible C programs.

C #else Preprocessor Directive FAQs

In C programming, the "#else" directive can be employed in conjunction with the "#ifdef" or "#ifndef" directives.

Yes, the #else directive can be used after either "#ifdef" or "#ifndef". It offers an alternative in cases where the macro is not defined for "#ifdef" or is defined for "#ifndef".

No, the #else directive is not compulsory in conditional compilation in C.

Yes, the #else statement is not mandatory in conditional compilation. It is possible to utilize only the #if . . . #endif construct without an #else section. When the condition is not met, the code within that block will not be compiled. Nevertheless, the #else directive serves a purpose in establishing a fallback behavior.

Multiple #else directives in C are not allowed within the same #if or #ifdef block. Only one #else directive can follow an #if or #ifdef directive. If multiple #else directives are encountered, it will result in a compilation error.

The C programming language does not allow for multiple #else directives within a single conditional block. Trying to include more than one #else directive before the #endif statement will result in a compilation error as the preprocessor anticipates only a single alternative path. To handle multiple conditions, the '#elif' directive can be utilized.

4) Can the #else directive be nested in C?

Yes, the #else directive can be nested within other conditional blocks, each requiring its individual #endif. This nesting capability enables intricate conditional logic, such as:

Example

# ifdef OUTER

// nested code block true

# else

// nested false

# endif

# else

// outer false

# endif

No, the #else directive cannot be used inside functions in C.

Yes, the #else directive can be utilized within functions in the C programming language. This directive can be positioned anywhere within the program, provided it is enclosed within a valid conditional compilation block.

Input Required

This code uses input(). Please provide values below: