What is the #endif directive in C?

In the C Programming Language, the #endif directive is used to define the following directives: #if, #ifdef, and #ifndef. Whenever the #endif directive is encountered in a program, it determines if the preprocessing of #if, #ifdef, or #ifndef has been completed successfully.

Syntax

#endif
#if (expression)  
// given that expression is true, if is executed. 
//code  
#endif  
#if expression  
//if code  

// instead of else if, we use elif in this directive. 
#elif expression
//elif code  


#else  
//else code

#endif  

Code

Let’s look at a simple coding example:

widget
#include <stdio.h>
#define NUMBER -2
int main() {
#if (NUMBER>0)
printf("Value of Number greater than 0 is: %d",NUMBER);
#else
printf("Value of Number less than 0 is: %d",NUMBER);
// #endif is used to determine the end the if condition.
#endif
return 0;
}

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved