Variable Name validity : Switch case MCQ



Switch case & Variable Name Validity :

This Program at a First Glance is error Free but It has one Serious Bug , Will you remove it without compiling it ?

#include<stdio.h>
void OS_Solaris_print()
{
    printf("Solaris - Sun Microsystemsn");
}
void OS_Windows_print()
{
    printf("Windows - Microsoftn");
}
void OS_HP-UX_print()
{
    printf("HP-UX - Hewlett Packardn");
}
int main()
{
    int num;
    printf("Enter the number (1-3):\n");
    scanf("%d",&num);
    switch(num)
    {
        case 1:
            OS_Solaris_print();
            break;
        case 2:
            OS_Windows_print();
            break;
        case 3:
            OS_HP-UX_print();
            break;
        default:
            printf("Hmm! only 1-3 \n");
            break;
    }
}

Output :

Compile Error

Explanation

Consider the above program and look into program carefully. You will find below statement inside the program.

case 3:
    OS_HP-UX_print();
    break

Now we can see name of the function name is not a valid identifier in C programming.

Re-commanded Tutorial : Rules for declaring valid identifiers in C programming.

Variable Name validity

OS_HP-UX_Print() is not a valid identifier or function name. Special character dash should not be used in identifier or variable name or in function name.

We can use following function name instead the one specified in the above example -

OS_HP_UX_print();