C LANGUAGE: PREPROCESSORS

Conditional compilation

Macro Meaning

(i)
#if (expression)
statements (A)
#else
statements (B)
#endif

If the given condition is true then the statements (A) will be compiled and executed otherwise statements (B) will be compiled and executed

(ii)
#ifdef macroname
statements (A)
#endif

If the specified macro is defined then the set of statements (A) are compiled otherwise not.

(iii)
#ifdef macroname
statements (A)
#else
statements  (B)
#endif

If the specified macro is defined then the set of statements (A) are compiled otherwise set of statements (B) are compiled

(iv)
#ifndef macroname
statements (A)
#endif

If the specified macro is not  defined then the set of statements (A) are compiled otherwise not.

(v)
#ifndef macroname
statements (A)
#else
statements (B)
#endif

If the specified macro is not defined then the set of statements (A) are compiled otherwise set of statements (B) are compiled

(vi)
#undef macroname

On execution of this statement the specified macro is undefined.

Examples:1

#if (expression)
statements (A)
#else
statements (B)
#endif

#include<stdio.h>
#include<conio.h>
#define a 10
 void main()
 {
     //clrscr();
     #if(a>1)
        printf("Hello");
     #else
        printf("Hi");
     #endif
 getch();
 }
/* Output */
Hello
#include<stdio.h>
#include<conio.h>
#define a 10
 void main()
 {
     //clrscr();
     #if(a>100)
        printf("Hello");
     #else
        printf("Hi");
     #endif
 getch();
 }
/* Output */
Hi

Example:2

#ifdef macroname
statements
#endif

#include<stdio.h>
 //#include<conio.h>
 #define a 10
 void main()
 {
     int i;
     //clrscr();
     printf("hello\n");
     #ifdef a
     for(i=1;i<=10;i=i+2)
    {
        printf("%d\n",i);
    }
    #endif
    printf("hi\n");
    return(0);
 }
/* Output */
hello
1
3
5
7
9
hi

Example:3

#ifdef macroname
statements
#else
statements
#endif

#include<stdio.h>
//#include<conio.h>
#define a 10
 int main()
 {
     int i;
     //clrscr();
     printf("hello\n");
     #ifdef a
     for(i=1;i<=10;i=i+2)
    {
        printf("%d\n",i);
    }
    #else
    for(i=2;i<=10;i=i+2)
    {
        printf("%d\n",i);
    }
    #endif
    printf("hi\n");
    return(0);
 }
/* Output */
hello
1
3
5
7
9
hi

Example:4

#ifndef macroname
statements
#endif

#include<stdio.h>
//#include<conio.h>
#define a 10
int main()
 {
     int i;
     //clrscr();
     printf("hello\n");
     #ifndef a
     for(i=1;i<=10;i=i+2)
    {
        printf("%d\n",i);
    }
    #endif
    printf("hi\n");
    return(0);
 }
/* Output */
hello
hi

Example:5

#ifndef macroname
statements
#else
statements
#endif

#include<stdio.h>
#include<conio.h>
#define a 10
int main()
 {
     int i;
     //clrscr();
     printf("hello\n");
     #ifndef a
     for(i=1;i<=10;i=i+2)
    {
        printf("%d\n",i);
    }
    #else
    for(i=2;i<=10;i=i+2)
    {
        printf("%d\n",i);
    }
    #endif
    printf("hi\n");
    return(0);
 }
/* Output */
hello
2
4
6
8
10
hi

Example:6

#undef macroname

#include<stdio.h>
//#include<conio.h>
#define a 10
int main()
 {
     int i;
     //clrscr();
     printf("hello\n");
     #ifdef a
     for(i=1;i<=10;i=i+2)
    {
        printf("%d\n",i);
    }
    #else
    for(i=2;i<=10;i=i+2)
    {
        printf("%d\n",i);
    }
    #endif
    printf("hi\n");
    #undef a
    printf("hello\n");
    #ifdef a
    for(i=1;i<=10;i=i+2)
    {
        printf("%d\n",i);
    }
    #else
    for(i=2;i<=10;i=i+2)
    {
        printf("%d\n",i);
    }
    #endif
    printf("hi\n");
    return(0);
 }
/* Output */
hello
1
3
5
7
9
hi
hello
2
4
6
8
10
hi

C Language Programming Tutorial

C Language Tutorial Home     Introduction to C Language     Tokens     If Condition      goto statement and Labelname     Switch Statements     For loop     While Loop     Do while loop     break and continue     Functions     Recursion     Inbuild Functions     Storage Classes     Preprocessor     Arrays     Pointers     Structures and Unions     File Handling     Projects