Friday, July 13, 2018

C #else

Description

In the C Programming Language, the #else directive provides an alternate action when used with the #if, #ifdef, or #ifndef directives. The preprocessor will include the C source code that follows the #else statement when the condition for the #if, #ifdef, or #ifndef directive evaluates to false.

The #else preprocessor directive evaluates the expression or condition if condition of #if is false. It can be used with #if, #elif, #ifdef and #ifndef directives.

Syntax:





#if expression
//if code
#else
//else code
#endif



Syntax with #elif:





#if expression
//if code
#elif expression
//elif code
#else
//else code
#endif



Note

  • The #else directive must be closed by an #endif directive.

C #else example

Let's see a simple example to use #else preprocessor directive.





#include <stdio.h>
#include <conio.h>
#define NUMBER 1
void main() {
#if NUMBER==0
printf("Value of Number is: %d",NUMBER);
#else
print("Value of Number is non-zero");
#endif
getch();
}



Output:

Value of Number is non-zero

Example

The following example shows how to use the #else directive in the C language:





/* Example using #else directive by c programming */

#include <stdio.h>

#define YEARS_OLD 12

int main()
{
#if YEARS_OLD < 10
printf("c programming is a great resource.\n");
#else
printf("TechOnTheNet is over %d years old.\n", YEARS_OLD);
#endif

return 0;
}



Here is the output of the executable program:

c programming is over 12 years old.




Instagram



No comments:

Post a Comment