Right Click to Search

Thursday, February 4, 2025

#ifdef statement : Conditional Compilation Directives (C Preprocessor)





#ifdef statement : Conditional Compilation Directives (C Preprocessor)

What it does ?
  • These Conditional Compilation Directives allow us to include certain portion of the code depending upon the output of constant expression 
  • Block is Called as Conditional Group
Syntax :

#ifdef MACRONAME   
    Statement_block;
#endif


Explanation :


  1. If the MACRONAME specified after #ifdef is defined previously in #define then statement_block is followed otherwise it is skipped
  2. We say that the conditional succeeds if MACRO is defined, fails if it is not.
Live Example 1 :

#include"stdio.h"
#define NUM 10
void main()
{
#ifdef NUM
      #define MAX 20    // Define another macro if MACRO NUM is defined 
#endif
printf("MAX number is : %d",MAX);
}
Output :
MAX Number is 20

Live Example 2 :

#include"stdio.h"
void main()
{
#ifdef MAX  #define MIN 90
#else
  #define MIN 100
#endif
printf("MIN number : %d",MIN);
}
Output :
MIN number : 100

Rules :
  1. The MACRONAME inside of a conditional can include preprocessing directives. 
  2. They are executed only if the conditional succeeds. 
  3. You can nest conditional groups inside other conditional groups, but they must be completely nested. 
  4. You cannot start a conditional group in one file and end it in another.


Bookmark & Share

Stumble
Delicious
Technorati
Twitter
Facebook

0 Comments:

Post a Comment

Your Feedback :This is Growing Site and Your Feedback is important for Us to improve the Site performance & Quality of Content.Feel Free to contact and Please Provide Name & Contact Email

 

Learn C Programming Copyright © 2010 LKart Theme is Designed by Lasantha, Free Blogger Templates