#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 https://www.youtube.com/watch?v=1FwlFR9PQDE]

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

Source Code: #undef Directive: C Program

#include<stdio.h>

#define iOS
#undef iOS

int main()
{
    #if defined(iOS)
        printf("I Love Apple Devices\n");
    #else
        printf("Macro iOS has been undefined\n");
    #endif // defined

    return 0;
}

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.

#include<stdio.h>

#define iOS

int main()
{
    #if defined(iOS)
        printf("I Love Apple Devices\n");
        printf("Gift me a latest iPhone!\n");
    #else
        printf("Macro iOS has been undefined\n");
    #endif // defined

    return 0;
}

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.

#include<stdio.h>

#define iOS

int main()
{
    #undef iOS
    #if defined(iOS)
        printf("I Love Apple Devices\n");
    #else
        printf("Macro iOS has been undefined\n");
    #endif // defined

    return 0;
}

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.

#include<stdio.h>

#define iOS

int main()
{
    int flag = 1;

    do
    {
        #if defined(iOS)
            printf("I Love Apple Devices\n");
            printf("This displays all your iPhone Device Details\n");
        #else
            printf("I'm inside do while block.\n");
        #endif // defined

        printf("Enter your choice? (0/1)\n");
        scanf("%d", &flag);

    }while(flag);


    #undef iOS

    #if defined(iOS)
        printf("I Love Apple Devices\n");
        printf("This displays all your iPhone Device Details\n");
    #else
        printf("Macro iOS has been undefined\n");
    #endif // defined

    return 0;
}

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

Conditional Compilation In C: #ifdef #else #endif

In this video tutorial lets learn about preprocessor command or directives like #ifdef, #else and #endif. These directives are used for conditional compilation.

How Does #ifdef Work?

The block of code between #ifdef and #endif works only if the macro name is defined orelse the compiler will skip the entire block of code from compiling.

When To Use #ifdef

1. When we want the compiler to skip certain part of the source code: For this we could even use multi-line comment, but if we already had multi-line comments in the code, we can’t enclose the source code and multi-line comments with another multi-line comment. Nesting of multi-line comments are not allowed in C programming language. So instead of commenting the code we could make use of #ifdef directive.

2. To write portable code: For example, we could write code both for iOS and non-iOS devices. Using #ifdef we could check the device OS and based on that deliver specific set of codes. For this we can use #ifdef

Video Tutorial: Conditional Compilation In C: #ifdef #else #endif


[youtube https://www.youtube.com/watch?v=J0nWQRqb3Fc]

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

Source Code: Conditional Compilation In C: #ifdef #else #endif

#include<stdio.h>

#define iOS

int main()
{

    #ifdef iOS
        printf("This is iOS Code\n");
    #else
        printf("This is code for Android Devices\n");
    #endif // iOS

    return 0;
}

Output:
This is iOS Code

#include<stdio.h>

int main()
{

    #ifdef iOS
        printf("This is iOS Code\n");
    #else
        printf("This is code for Android Devices\n");
    #endif // iOS

    return 0;
}

Output:
This is code for Android Devices

In above programs, if iOS macro name is defined, then the set of code present inside #ifdef block gets compiled and executed. If macro name iOS is not defined, then the code inside #else block gets compiled and executed.

#include<stdio.h>
#define iOS

int main()
{

    #ifdef iOS
        printf("This is iOS Code\n");
    #endif // iOS

    printf("This is code for all non iOS Devices\n");

    return 0;
}

Output:
This is iOS Code
This is code for all non iOS Devices

#include<stdio.h>

int main()
{

    #ifdef iOS
        printf("This is iOS Code\n");
    #endif // iOS

    printf("This is code for all non iOS Devices\n");

    return 0;
}

Output:
This is code for all non iOS Devices

Note: using #else is optional. You can just use #ifdef and #endif.

This is similar to if else condition, but here the block of code which do not match the criteria doesn’t even get compiled. It’s treated like regular comments.

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