#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

Switch Case Default In C Programming Language

In this video tutorial lets learn how switch case decision control statement works and its syntax in C Programming Language.

Related Read:
Simple Calculator Program using Switch Case: C
break Statement In C Programming Language
Continue Statement In C Programming Language

Note: The condition in switch must be a integer constant or an expression which evaluates to an integer constant.

Video Tutorial: Switch Case Default In C Programming Language


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

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

Source Code: Switch Case Default In C Programming Language

#include<stdio.h>

int main()
{
    char choice;

    printf("Enter your choice\n");
    scanf("%c", &choice);

    switch(choice)
    {
        case 'a':
        case 'A': printf("You typed A or a\n");
                  break;

        case 'b':
        case 'B': {
                   printf("You typed B or b\n");
                   break;
                  }

        default: printf("Your selection is wrong!\n");
    }

    return 0;
}

Output 1
Enter your choice
B
You typed B or b

Output 2
Enter your choice
b
You typed B or b

Here we’ve not specified keyword break after case ‘a’ and case ‘b’, that means if user enters a or A, for both whatever is present in case ‘A’ gets executed and then control exits switch case when it encounters break statement. Similarly for ‘b’ and ‘B’.

Also note that curly braces are optional here. Infact its unnecessary to use curly braces in switch case statements.

#include<stdio.h>

int main()
{
    int choice;

    printf("Enter your choice\n");
    scanf("%d", &choice);

    switch(choice)
    {
        case 1: printf("This is first statement\n");

        case 2: printf("This is second statement\n");

        default: printf("Your selection is wrong!\n");
    }

    return 0;
}

Output 1
Enter your choice
1
This is first statement
This is second statement
Your selection is wrong!

Output 2
Enter your choice
2
This is second statement
Your selection is wrong!

Here we’re not using break after any case, so it’s printing statements present in all the cases.

#include<stdio.h>
int main()
{
    int choice;

    printf("Enter your choice\n");
    scanf("%d", &choice);

    switch(choice)
    {
        case 1: printf("This is first statement\n");
                break;

        case 2: printf("This is second statement\n");
                break;

        default: printf("Your selection is wrong!\n");
    }

    return 0;
}

Output 1
Enter your choice
1
This is first statement

Output 2
Enter your choice
2
This is second statement

Output 3
Enter your choice
a
Your selection is wrong!

#include<stdio.h>
int main()
{
    int choice;

    printf("Enter your choice\n");
    scanf("%d", &choice);

    switch(choice)
    {
        case 2: printf("Apple\n");
                break;

        case 1: printf("IBM\n");
                break;

        default: printf("Your selection is wrong!\n");
    }

    return 0;
}

Output 1
Enter your choice
1
IBM

Here order doesn’t matter. As you can see from above example, case 2 comes before case 1.

#include<stdio.h>
int main()
{
    int choice;

    printf("Enter your choice\n");
    scanf("%d", &choice);

    switch(choice)
    {
        printf("List of companies\n");
        case 2: printf("Apple\n");
                break;

        case 1: printf("IBM\n");
                break;

        default: printf("Your selection is wrong!\n");
    }

    return 0;
}

Output 1
Enter your choice
1
IBM

All the code must go inside any of the cases orelse those code will not have any effect. In above program the printf statement with “List of companies” doesn’t get printed and it doesn’t even throw any error.

Note:
1. default case is optional.
2. Value of cases in switch must always be unique.
3. switch can not be used with float, double.
4. Switch, case, default are all builtin keywords.
5. continue does not take the control back to the start of switch.

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

goto Keyword In C Programming Language

In this video tutorial lets learn how goto keyword works and its syntax in C Programming Language.

Note: As for as possible lets avoid using goto keyword and lets write programs using for loop, while loop, do while loop, continue and break statements itself. Lets learn usage of goto keyword just for the sake of knowing about it.

Related Read:
Continue Statement In C Programming Language
break Statement In C Programming Language

Video Tutorial: goto Keyword In C Programming Language


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

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

Source Code: goto Keyword In C Programming Language

#include<stdio.h>

int main()
{
    int count = 1;

    while(count <= 10)
    {
        if(count == 6)
            goto six; // six is the label

        printf("%d IBM\n", count++);
    }

    six:
        printf("This is sixth loop\n");

    return 0;
}

Output
1 IBM
2 IBM
3 IBM
4 IBM
5 IBM
This is sixth loop

Working of goto Statement in C Programming Language

goto statement should always have a label associated with it. In above program label is six. Once count value is 6, control encounters goto six; Control searches for label six inside the program. Once it gets the label six, it executes the statements associated with label six.

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

do-while Loop In C Programming Language

In this video tutorial lets learn about the general syntax and working of do-while loop in C programming language.

Related Read:
while loop in C programming
For Loop In C Programming Language
Using Scanf in C Program

Video Tutorial: do-while Loop In C Programming Language


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

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

Source Code: do-while Loop In C Programming Language

#include<stdio.h>

int main()
{
    int count = 1;

    do
    {
        printf("Apple\n");
        printf("IBM\n");

    }while(count > 5);

    return 0;
}

Output
Apple
IBM

Note: Even though the while condition is false, the code inside do block gets executed atleast once.

Source Code: do-while Loop In C Programming Language

#include<stdio.h>

int main()
{
    char ch;

    do
    {
        printf("Apple\n");
        printf("IBM\n");

        printf("Do you want to continue?(y/n)");
        scanf("%c", &ch);

        fflush(stdin);

    }while(ch == 'y');

    return 0;
}

Output
Apple
IBM
Do you want to continue?(y/n)y
Apple
IBM
Do you want to continue?(y/n)y
Apple
IBM
Do you want to continue?(y/n)y
Apple
IBM
Do you want to continue?(y/n)y
Apple
IBM
Do you want to continue?(y/n)n

Working of do-while Loop

Unlike in while and for loop, in do-while loop the statements inside do block gets executed atleast once. After executing the statements present in do block atleast once, the condition present in while is checked. If while condition is true, then the block of code in do{} gets executed once again. If condition in while is false then the control exists do-while loop.

Note: Since we might start to input information from the keyboard repeatedly inside do-while block, scanf() method keeps checking the input buffer. And often times it gets confused with the input buffer and thinks that the user has pressed the enter key. To avoid that we flush out the previous buffer present in input device(ex: keyboard) using function fflush(). fflush takes stdin as argument, so that it can clear the buffer of standard input device. fflush(stdin);

Source Code: Infinite Looping in do-while Loop – In C Programming Language

#include<stdio.h>

int main()
{
    do
    {
        printf("Apple\n");
        printf("IBM\n");

    }while(1);

    return 0;
}

Output
do-while loop gets into infinite loop as the condition in while is non-zero number, which means the condition is always true.

Apple
IBM

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