#else in C | How #else Directive work?

#else in C | How #else Directive work?
Techiio-author
Written by Shuvhojit DebFebruary 12, 2022
5 min read
C
1 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 1 VIEWS SHARE
Techiio-author
Shuvhojit Deb

Full Stack Developer

In this article, we'll discuss the #else in C.

Introduction

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#.

Syntax

#if _condition_
// Statements to be executed when condition returns TRUE
#else
// statements to be executed when condition in #if results to false.
#endif

How #else Directive work in C?

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:

  • Macros
  • File Inclusion
  • Conditional Compilation
  • Other 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:

  • Expressions must be integral. It can also include integer constants, character constants, and the defined operator.
  • sizeOf or typecast operator cannot be used in the expression.
  • All the types such as int, long, or unsigned long are translated in the same manner.
  • The expression should not include any query related to the environment in which the program is running.

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

/* 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.
C Language
else
software
programming
1 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 1 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance