EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

#else in C

Home » Software Development » Software Development Tutorials » C Programming Tutorial » #else in C

#else in C

Introduction to #else in C

The following article provides an outline for #else in C. Else is a directive in C programming language that helps to provide the statements those needs to be executed when the conditions given using #if, #ifdef or #ifndef directives evaluates to false. Once the condition given in these directives evaluates to false, #else directives provides an alternate statements to be executed. It is a part of preprocessor directive since it is called by the compiler automatically before actual compilation starts. Before a C program is compiled by the compiler source code is processed thus 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 of #else in C

Preprocessors is a feature provided in C to process the source code written by the programmer before its actual compilation is done. Before the program is passed through a preprocessor compiler passes the code through the preprocessor where specific instructions such as directives are looked for in the C program known as preprocessor directives that can be easily understood by the preprocessor. These preprocessor directives are must begin with (#) sign.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Preprocessor is that part of the compiler which executes essential operations in the given code before the compiler actually compiles it. The transformations performed by the preprocessors are lexical which tells that the output of the preprocessor is in text form.

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

Example:

Code:

#if 4>5
printf("Statements inside if block")
#else
printf("Statements inside else block")

Here # specifies that it is a preprocessor directive and is compiled using the preprocessor before actual code is sent for the compilation to the compiler. One can use macro defined in the program for the conditions in the if directive and those macros needs to be defined using #define directive in C.

Popular Course in this category
C Programming Training (3 Courses, 5 Project)3 Online Courses | 5 Hands-on Projects | 34+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,630 ratings)
Course Price

View Course

Related Courses
C++ Training (4 Courses, 5 Projects, 4 Quizzes)Java Training (40 Courses, 29 Projects, 4 Quizzes)

How #else Directive work in C?

Preprocessors refers 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.

There are various preprocessor directives that 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 generates an expanded source file with 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 statements need to be executed when the condition given using #if, #ifdef or #ifndef. Whenever the condition returns false compiler sends the control directly to the #else block statement.

There are certain rules need to be followed for declaring conditional expression:

  • Expressions must be of 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 on 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.

Examples of #else in C

Given below are the examples mentioned :

Example #1

In this example we will use #If directive to declare a condition for the execution for the statements. And if the condition results to false the statements given in else block will be executed. Here we will use LIMIT macro name defined using #define directive.

Code:

#include <stdio.h>
#define LIMIT 5
int main()
{
int number;
printf("Enter a number : ");
scanf("%d",&number);
#if number < LIMIT
printf("Entered Number is less than the limit \n");
#else
printf("Entered Number is greater than the limit \n");
#endif
return 0;
}

Output:

#else in c 1

Example #2

In this example we will see if the student has passed or not using PASS variable defined using #define directive. We will compare the marks of the student being entered to the PASS macro name and print the result for that particular student.

Code:

#include <stdio.h>
#define MARKS 50
int main()
{
#ifdef MARKS
printf("MARKS macro has been defined \n");
#endif
#if MARKS >90
printf("Student has scored GRADE A");
#elif MARKS >60
printf("Student has scored GRADE B");
#else
printf("Student has scored GRADE C");
#endif
return 0;
}

Output:

#else in c 2

Conclusion

While working with preprocessor directives in a large C program one can declare conditional statements for executing some statements using #ifdef or #if or #ifndef directives. Thus #else directive here provides the block to be executed when the condition given in above block results to false.

Recommended Articles

This is a guide to #else in C. Here we discuss the introduction to #else in C, how #else directive work along with programming examples respectively. You may also have a look at the following articles to learn more –

  1. C ftell()
  2. Preprocessor Directives in C
  3. Best C Compilers
  4. While Loop in C

All in One Software Development Bundle (600+ Courses, 50+ projects)

600+ Online Courses

50+ projects

3000+ Hours

Verifiable Certificates

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
C Programming Tutorial
  • Control Statement
    • Control Statements in C
    • If Statement in C
    • If-else Statement in C
    • Else if Statement in C
    • Nested if Statement in C
    • #else in C
    • Structure Padding in C
    • Nested Structure in C
    • Continue Statement in C
    • Break Statement in C
    • Switch Statement in C
    • Goto Statement in C
  • Basic
    • Introduction to C
    • What is C
    • Career in C Programming
    • Advantages of C
    • How to Install C
    • Best C Compilers
    • Data Types in C
    • Variables in C
    • C Keywords
    • C Command
    • Command Line Arguments in C
    • C Literals
    • Constants in C
    • Unsigned Int in C
    • String in C
  • Pointers
    • Pointers in C
    • Null pointer in C
    • Function Pointer in C
    • Double Pointer in C
    • Void Pointer in C
    • Const Pointer in C
    • Dangling Pointers in C
    • Pointer Arithmetic in C
  • Operators
    • C Operators
    • Arithmetic Operators in C
    • Relational Operators in C
    • Assignment Operators in C
    • Logical Operators in C
    • Conditional Operator in C
    • Modulus Operator in C
    • Ternary Operator in C
    • Address Operator in C
    • Unary Operator in C
    • Operators Precedence in C
    • Left Shift Operator in C
  • Loops
    • Loops in C
    • For Loop in C
    • While Loop in C
    • Do While Loop in C
    • Nested Loop in C
    • Infinite Loop in C 
  • Function
    • Math Functions in C
    • Hashing Function in C
    • Recursive Function in C
    • Power Function in C
    • fputs in C
    • C puts() Function
    • fprintf() in C
    • fseek() in C
    • Stderr in C
    • ASCII Value in C
    • strcat() in C
    • Inline Function in C
    • sizeof() in C
    • Function Prototype in C
    • C ftell()
  • Array
    • Arrays in C Programming
    • 2-D Arrays in C
    • 3D Arrays in C
    • Multidimensional Array in C
    • Array Functions in C
    • Strings Array in C
  • Sorting
    • Sorting in C
    • Heap Sort in C
  • Advanced
    • Constructor in C
    • Encapsulation in C
    • C Storage Classes
    • Static Keyword in C
    • File Handling in C
    • Queue in C
    • Hexadecimal in C 
    • typedef in C
    • Memory Allocation in C
    • Linked List in C
    • Volatile in C
    • Tokens in C
    • Expression in C
    • Regular Expression in C
    • Error Handling in C
    • Types of Errors in C
    • Preprocessor in C
    • Preprocessor Directives in C
    • fscanf() in C
    • #Pragma in C
    • #ifndef in C
    • #undef in C
    • Macros in C
  • C programs
    • Patterns in C Programming
    • Star Patterns in C
    • Number Patterns in C
    • Swapping in C
    • Reverse Number in C
    • Palindrome in C Program
    • Factorial in C
    • Fibonacci Series in C
    • Square Root in C
    • Random Number Generator in C
    • Prime Numbers in C
    • Escape Sequence in C
    • Reverse String in C
    • Leap Year Program in C
    • Anagram Program in C
    • Strong Number in C
    • String Concatenation in C
    • C Programming Matrix Multiplication
    • Decimal to Octal in C
    • Expression Evaluation in C
    • Decimal to Hexadecimal in C
  • Interview question
    • C Programming Interview Questions

Related Courses

C Programming Training Course

C++ Training Course

Java Training Course

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - C Programming Training Course Learn More