- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is conditional compilation in C language?
What is conditional compilation in C language?
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 −
# include <stdio.h> # define a 10 void main(){ #ifdef a printf("
Hello I am here.."); #endif #ifndef a printf("
Not defined "); #else printf("
R u There "); #endif }
Output
When the above program is executed, it produces the following result −
Hello I am here.. R u There