What will be the output of the following program?#include<stdio.h> int main() { int i=4; switch(i) { default: printf("This is default."); case 1: printf("This is case 1"); break; case 2: printf("This is case 2"); break; case 3: printf("This is case 3"); } return 0; }Select one:This is default.This is case 1This is case 3This is default.This is case 1
Question
What will be the output of the following program?#include<stdio.h> int main() { int i=4; switch(i) { default: printf("This is default."); case 1: printf("This is case 1"); break; case 2: printf("This is case 2"); break; case 3: printf("This is case 3"); } return 0; }Select one:This is default.This is case 1This is case 3This is default.This is case 1
Solution
The output of the program will be "This is default." The reason is that the switch statement checks the value of the variable 'i'. Since 'i' is 4 and there is no case for 4, it goes to the default case. After printing "This is default.", it does not find a break statement, so it continues to the next case, which is case 1, and prints "This is case 1". Therefore, the output is "This is default.This is case 1".
Similar Questions
What is the output of the following C Program?
What will be the output of the following code?
what will be the output of the c program?
What will be the output of the following C++ code?
What will be the output of the following program?Note: This question helps in clearing the AMCAT exam
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.