# What is conditional compilation in C language?

CServer Side ProgrammingProgramming

In C programming language, several directives control the selective compilation of portions of the program code. They are as follows −

• #if
• #else
• #elif
• #endif

The general form of #if is as follows −

#if constant_expression
statement sequence
#endif

#else works much like the C keyword else.

#elif means "else if" and establishes an if else-if compilation chain.

Amongst other things, #if provides an alternative method of "commenting out" code.

For example,

#if 0
printf("#d", total);
#endif

Here, the compiler will ignore printf("#d", total);

#ifdef and #ifndef

#ifdef means "if defined", and is terminated by an #endif.

#indef means "if not defined".

#undef

#undef removes a previously defined definition.

#line

#line changes the contents of __LINE__ which contains the line number of the currently compiled code and __FILE__ which is a string that contains the name of the source file being compiled. Both of which are predefined identifiers in the compiler.

#pragma

The #pragma directive is an implementation-defined directive which allows the various instructions to be given to the compiler.

## Example

Following is the C program to demonstrate #ifdef, #ifndef , #else and #endif

Live Demo

# include <stdio.h>
# define a 10
void main(){
#ifdef a
printf("\n Hello I am here..");
#endif
#ifndef a
printf("\n Not defined ");
#else
printf("\n R u There ");
#endif
}

## Output

When the above program is executed, it produces the following result −

Hello I am here..
R u There
Updated on 02-Sep-2021 13:19:11