#undef Directive: C Program

In today’s video tutorial lets see how to use #undef preprocessor directive.

Related Read:
C Program To Find Area of Circle Using Macro Expansion
Conditional Compilation In C: #if #elif #else #endif

What Does #undef Do?

#undef removes or undefines a macro name which is previously created or defined using #define directive.

Syntax

#undef MACRO_NAME

Note: If a Macro template is associated with a Macro expansion, you need not mention the macro expansion to undefine the macro. You simple undefine using this syntax: #undef MACRO_NAME

Video Tutorial: #undef Preprocessor Directive: C Program



YouTube Link: https://www.youtube.com/watch?v=1FwlFR9PQDE [Watch the Video In Full Screen.]

Source Code: #undef Directive: C Program

  1. #include<stdio.h>  
  2.   
  3. #define iOS  
  4. #undef iOS  
  5.   
  6. int main()  
  7. {  
  8.     #if defined(iOS)  
  9.         printf("I Love Apple Devices\n");  
  10.     #else  
  11.         printf("Macro iOS has been undefined\n");  
  12.     #endif // defined  
  13.   
  14.     return 0;  
  15. }  

Output:
Macro iOS has been undefined

In above source code we’re undefining the Macro iOS immediately after defining it. So defined(iOS) returns false or 0. So code inside #else block gets compiled and executed.

  1. #include<stdio.h>  
  2.   
  3. #define iOS  
  4.   
  5. int main()  
  6. {  
  7.     #if defined(iOS)  
  8.         printf("I Love Apple Devices\n");  
  9.         printf("Gift me a latest iPhone!\n");  
  10.     #else  
  11.         printf("Macro iOS has been undefined\n");  
  12.     #endif // defined  
  13.   
  14.     return 0;  
  15. }  

Output:
I Love Apple Devices
Gift me a latest iPhone!

Here Macro iOS is defined, so defined(iOS) returns true. That’s why block of code inside #if directive gets compiled and executed.

  1. #include<stdio.h>  
  2.   
  3. #define iOS  
  4.   
  5. int main()  
  6. {  
  7.     #undef iOS  
  8.     #if defined(iOS)  
  9.         printf("I Love Apple Devices\n");  
  10.     #else  
  11.         printf("Macro iOS has been undefined\n");  
  12.     #endif // defined  
  13.   
  14.     return 0;  
  15. }  

Output:
Macro iOS has been undefined

Even though #define iOS is defined at global level you can undefine it inside main() method.

Note: defined() is a preprocessor operator which returns true if the passed macro name is defined orelse returns false or zero if its not defined.

Instead of defined() preprocessor operator you could even use #ifdef or #ifndef to check if the Macro template or Macro name is defined or not defined.

  1. #include<stdio.h>  
  2.   
  3. #define iOS  
  4.   
  5. int main()  
  6. {  
  7.     int flag = 1;  
  8.   
  9.     do  
  10.     {  
  11.         #if defined(iOS)  
  12.             printf("I Love Apple Devices\n");  
  13.             printf("This displays all your iPhone Device Details\n");  
  14.         #else  
  15.             printf("I'm inside do while block.\n");  
  16.         #endif // defined  
  17.   
  18.         printf("Enter your choice? (0/1)\n");  
  19.         scanf("%d", &flag);  
  20.   
  21.     }while(flag);  
  22.   
  23.   
  24.     #undef iOS  
  25.   
  26.     #if defined(iOS)  
  27.         printf("I Love Apple Devices\n");  
  28.         printf("This displays all your iPhone Device Details\n");  
  29.     #else  
  30.         printf("Macro iOS has been undefined\n");  
  31.     #endif // defined  
  32.   
  33.     return 0;  
  34. }  

Output:
I Love Apple Devices
This displays all your iPhone Device Details
Enter your choice? (0/1)
1
I Love Apple Devices
This displays all your iPhone Device Details
Enter your choice? (0/1)
1
I Love Apple Devices
This displays all your iPhone Device Details
Enter your choice? (0/1)
0
Macro iOS has been undefined

In above program we are defining iOS macro globally. Inside main method we use do while loop and keep executing #if #else #endif directives. Since iOS is defined, the block of code inside #if gets executed each time. Once user enters 0 as input, the control exits do while loop execution. Next it encounters #undef iOS, so defined(iOS) now returns false or 0, hence code inside #else directive gets executed.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Preprocessor Operator: defined

In this video tutorial lets see how to use preprocessor operator “defined” in C programming language.

How Does ‘defined’ preprocessor operator work?

defined preprocessor operator can only be used with #if and #elif directives.

If the Macro name passed to define is actually defined, then it returns true or else it’ll return false.

Syntac of “defined”

  1. #if( defined(macro_name1) )  
  2.   // Some Set of Code  
  3. #elif( defined(macro_name2) )  
  4.   // Some More Set of Code  
  5. #endif  

OR

  1. #if defined(macro_name1)   
  2.   // Some Set of Code  
  3. #elif defined(macro_name2)   
  4.   // Some More Set of Code  
  5. #endif  

Video Tutorial: C Preprocessor Operator: defined



YouTube Link: https://www.youtube.com/watch?v=IDiV22rMPBk [Watch the Video In Full Screen.]

Source Code: C Preprocessor Operator: defined

  1. #include<stdio.h>  
  2.   
  3. #define PI 3.14159265358979323846  
  4.   
  5. #define AREA  
  6.   
  7. void area_circle(float r);  
  8. void circumference_circle(float r);  
  9.   
  10. int main()  
  11. {  
  12.     float r;  
  13.   
  14.     printf("Enter radius of Circle\n");  
  15.     scanf("%f", &r);  
  16.   
  17.     #if defined(AREA)  
  18.         area_circle(r);  
  19.     #elif defined(CIRCUM)  
  20.         circumference_circle(r);  
  21.     #else  
  22.         printf("Code To Be Implemented\n");  
  23.   
  24.     #endif // defined  
  25.   
  26.     return 0;  
  27. }  
  28.   
  29. void area_circle(float r)  
  30. {  
  31.     printf("Area of Circle is %f\n", (PI * r * r) );  
  32. }  
  33.   
  34. void circumference_circle(float r)  
  35. {  
  36.     printf("Circumference of Circle is %f\n", (2 * PI * r) );  
  37. }  

Output:
Enter radius of Circle
5
Area of Circle is 78.539816

In above source code AREA macro template or macro name is defined. So defined(AREA) returns true in #if condition. Hence the code inside #if block gets compiled and executed.

  1. #include<stdio.h>  
  2.   
  3. #define PI 3.14159265358979323846  
  4.   
  5. #define CIRCUM  
  6.   
  7. void area_circle(float r);  
  8. void circumference_circle(float r);  
  9.   
  10. int main()  
  11. {  
  12.     float r;  
  13.   
  14.     printf("Enter radius of Circle\n");  
  15.     scanf("%f", &r);  
  16.   
  17.     #if defined(AREA)  
  18.         area_circle(r);  
  19.     #elif defined(CIRCUM)  
  20.         circumference_circle(r);  
  21.     #else  
  22.         printf("Code To Be Implemented\n");  
  23.   
  24.     #endif // defined  
  25.   
  26.     return 0;  
  27. }  
  28.   
  29. void area_circle(float r)  
  30. {  
  31.     printf("Area of Circle is %f\n", (PI * r * r) );  
  32. }  
  33.   
  34. void circumference_circle(float r)  
  35. {  
  36.     printf("Circumference of Circle is %f\n", (2 * PI * r) );  
  37. }  

Output:
Enter radius of Circle
5
Circumference of Circle is 31.415927

Here macro name AREA is not defined, hence code inside #if gets skipped. Whereas macro CIRCUM is defined, so the code inside #elif gets compiled and executed.

If neither AREA and nor CIRCUM is defined, then code inside #else block gets compiled and executed.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert