C Tutorial
Also called conditional directive which is used to check whether particular fragment of code to be included or not based on conditional expression before compilation.
c ifdef directive is similar like if statement and difference is if statement is used to control program execution sequence but ifdef directive is used to control including chunk of code in current program.
Using ifdef directive, we can use different logic in the same program based on environment (software and hardware etc).
We can also control to avoid chuck of code in the current program using ifdef directive.
ifdef directive syntax,
#ifdef condition //chunk of code #endif
Let us see the example program for ifdef directive,
#include<stdio.h> #define PI 3.14 int compute_area(int radius) { return 3.14*radius*radius; } void main() { int area=0, radius=10; #ifdef PI area = compute_area(radius); #endif printf("\nArea of circle: %d", area); }
Output:
$ cc ifdef_directive.c $ ./a.out Area of circle: 314
Above program prints 0 if we comment define directive, because PI will not be defined and compute_area function will not be called.
#include<stdio.h> int compute_area(int radius) { return 3.14*radius*radius; } void main() { int area=0, radius=10; #ifdef PI area = compute_area(radius); #endif printf("\nArea of circle: %d", area); }
Output:
$ cc ifdef_directive.c $ ./a.out Area of circle: 0
We can also use #else directive with #ifdef directive similar like if else statements in c program.
#include<stdio.h> #define PI 3.14 int compute_area(int radius) { return 3.14*radius*radius; } void main() { int area=0, radius=10; #ifdef PI area = compute_area(radius); #else area = 200; #endif printf("\nArea of circle: %d", area); }
Output:
$ cc ifdef_directive.c $ ./a.out Area of circle: 314
if PI is not defined using #define directive, program will return area is 200 instead of actual computation value or default 0.
#include<stdio.h> //#define PI 3.14 int compute_area(int radius) { return 3.14*radius*radius; } void main() { int area=0, radius=10; #ifdef PI area = compute_area(radius); #else area = 200; #endif printf("\nArea of circle: %d", area); }
Output:
$ cc ifdef_directive.c $ ./a.out Area of circle: 200
This elif is similar like else if statements.
ifdef directive condition fails then checks elif condition and includes chunk of code if elif condtion is true.
elif directive syntax,
#ifdef condition1 //chunk of code1 #elif conditon2 //chunk of code2 #elif condition3 //chunk of code3 #elif ..... .... #else //chunk of code defualt #endif
Returns actual area computation value if PI is defined.
#include<stdio.h> #define PI 3.14 #define AREA 300 int compute_area(int radius) { return 3.14*radius*radius; } void main() { int area=0, radius=10; #ifdef PI area = compute_area(radius); #elif AREA area = AREA; #else area =200; #endif printf("\nArea of circle: %d", area); }
Output:
$ cc ifdef_directive.c $ ./a.out Area of circle: 314
Returns area is 300 if PI is not defined and only AREA is defined.
#include<stdio.h> //#define PI 3.14 #define AREA 300 int compute_area(int radius) { return 3.14*radius*radius; } void main() { int area=0, radius=10; #ifdef PI area = compute_area(radius); #elif AREA area = AREA; #else area =200; #endif printf("\nArea of circle: %d", area); }
Output:
$ cc ifdef_directive.c $ ./a.out Area of circle: 300
Returns default area is 200 if PI and AREA macros are not defined.
#include<stdio.h> //#define PI 3.14 //#define AREA 300 int compute_area(int radius) { return 3.14*radius*radius; } void main() { int area=0, radius=10; #ifdef PI area = compute_area(radius); #elif AREA area = AREA; #else area =200; #endif printf("\nArea of circle: %d", area); }
Output:
$ cc ifdef_directive.c $ ./a.out Area of circle: 200
C Tutorial
Privacy Policy | Copyright2020 - All Rights Reserved. | Contact us
| Report website issues in Github
| Facebook page
| Google+ page