Programming with C

⌘K
  1. Home
  2. Docs
  3. Programming with C
  4. Input/Output statements
  5. Pre-processor directives
  6. Conditional Compilation directive

Conditional Compilation directive

Conditional compilation means that a part of a program is compiled only if a certain condition comes out to be true

#if – #endif

Syntax

#if constant_exp
      statements-set
#endif

#if – #else – #endif

Syntax

#if constant_exp
    statements-set1
#else
    statements-set2
#endif

#if – #elif – #endif

Syntax

#if constant_exp1
   statement-set1
#elif constant_exp2
    statement-set 2
#endif

#ifdef – #else – #endif

Syntax

#ifdef identifier
    statement-set1
#elif constant-exp
   statement-set2
#endif

#ifndef – #endif

Syntax

#ifndef identifier
    statement-set1
#endif

#ifdef – #else – #endif

Syntax

#ifdef identifier
    statement-set1
#elif constant-exp
   statement-set2
#endif

#ifndef – #elif – #endif

Syntax

#ifndef identifier
   statements-set1
#elif constant-exp
   statements-set2
#endif

#ifndef – #else – #endif

Syntax

#ifndef identifier
   statement –set1
#else
   statement-set2
#endif

Example

#include <stdio.h>
#define x 10      
int main()
{
    #ifdef x
      printf("hello\n");      // this is compiled as  x is defined
   #else
      printf("bye\n");       // this isn't compiled
   #endif
   
   return 0;
}

Output

hello

Views: 0

How can we help?

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments