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

Rules for Constructing int, float, char constants: C

We’ve already learnt how to use int, float and char in our previous video tutorial. Now lets learn the rules to construct integer, float/real, character constants in C programming language.

Related Read:
Rules for Constructing Variable Names: C

Rules for Constructing int, float, char constants: C


[youtube https://www.youtube.com/watch?v=4o-ZJfi559Q]

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


Rules for Constructing Integer Constants

1. It must have atleast 1 digit. We can have anything from 0 to 9.
2. If user enters decimal point, it’ll be discarded by the program and only the integer part will be stored in the int variable.
3. We can assign positive or negative digit by appending the digit with + or – symbol. If nothing is specified it’ll be considered as positive.
4. Comma, space etc are not allowed.
5. Allowed range for integer constant is -2147483648 to +2147483647 for Visual Studio and gcc compilers. For Turbo C / C++ compilers allowed range for integer constants is -32768 to +32767.

Invalid Integer Constants
10,000
10 000
999999999999

Rules for Constructing Real Constants

1. It must have atleast 1 digit. Anything from 0 to 9 is allowed. If decimal value is not specified compiler will insert 0s for decimal value.
2. It can be either positive or negative value. If + or – symbol is not specified, it’ll be considered as positive.
3. No comma or space allowed.

Rules for Constructing Character Constants

1. Character constant must have only single alphabetic or Single digit or special symbol and must be enclosed in single quote.

Invalid Integer Constants
“A”
“Microsoft”
‘Apple’
‘123’
“$%^”