Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
C #else
2.1.
Syntax
3.
C #else example
4.
FAQs
4.1.
What are # preprocessor operators In C?
4.2.
What is a macro in a C preprocessor?
4.3.
Why is the #define directive used?
4.4.
What is __ LINE __ in C?
4.5.
What are the types of C preprocessors?
5.
Conclusion 
Last Updated: Mar 27, 2024
Easy

C #else

Author Shivam Verma
0 upvote
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

The #else is a preprocessor directive. Let's understand what a preprocessor directive is? Preprocessor directives are lines in a program that begins with the symbol #, distinguishing them from standard source code text. The compiler utilizes them to process some programs before compiling them. Preprocessor directives alter the source code's content, resulting in a new source code without these directives.

Also see: C Static Function, and Tribonacci Series

C #else

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

Syntax

The #else directive in C has the following syntax:

#if expression  
//statements 
#else  
//statements 
#endif  

The syntax for the #else directive with #elif is as follows:

#if expression  
//statements 
#elif expression  
//statements 
#else  
//statements 
#endif  

Note: The #else directive must be closed by an #endif directive.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

C #else example

Here is a C program that shows how to use the #else directive in the C language:

#include <stdio.h>
#define AGE 20
int main()
{
   #if AGE<18
   printf("You are not an adult.\n");
   #else
   printf("You are an adult\n");
   #endif
   return 0;
}

Output

You are an adult

 

You can also read about dynamic array in c and Short int in C Programming

FAQs

What are # preprocessor operators In C?

The stringizing operator(#) converts the preceding input into a quoted string.

What is a macro in a C preprocessor?

A macro is a preprocessor directive that allows you to replace tokens in your source code. The #define statement is used to build macros.

Why is the #define directive used?

The #define directive specifies values or macros that the preprocessor will use to change the program source code before it is generated. Because preprocessor definitions are substituted before the compiler acts on the source code, any errors that are introduced by #define are challenging to trace.

What is __ LINE __ in C?

__LINE__ is a preprocessor macro that expands to the source file's current line number as an integer. When writing debugging code or generating log statements, __LINE__ comes in useful.

What are the types of C preprocessors?

There are mainly four types of preprocessor directives:

  • Macros.
  • File Inclusion.
  • Conditional Compilation.
  • Other directives.

Conclusion 

In this article, we have extensively discussed the #else preprocessor directive.

We hope that this article has helped you enhance your knowledge regarding the #else preprocessor directive and if you would like to learn more, check out our article on Preprocessors in C.

Refer to our guided paths on the Coding Ninjas Studio platform to learn more about DSA, DBMS, Competitive Programming, Python, Java, JavaScript, etc.

Refer to the links problemstop 100 SQL problemsresources, and mock tests to enhance your knowledge.

For placement preparations, visit interview experiences and interview bundle.

Do upvote our blog to help other ninjas grow. Happy Coding!

Previous article
C #if
Next article
C #error
Live masterclass