Day#19 'C' Coding Challenge

Day#19 'C' Coding Challenge

What is the output of the below program?

#include <stdio.h> int choice = 1; void main() { switch(choice) { case 1: printf("Case 1"); case 0: printf("Case 0"); break; default: printf("default"); } }

----------------------------------------------------

The above code tests below scenarios:

1. Switch statement
2. Switch case execution when break is missing

----------------------------------------------------

As the choice is 1, when the switch execution starts, the execution goes to case 1 and "Case 1" shall be printed
There should be a break statement after case 1 but it is missing, so, the execution continues to case 0 and "Case 0' shall also be printed.
In case if break statement was present after case 1, only "Case 1" would have got printed
During programming, the programmers might miss break statement by mistake, it could be intentional in some cases.

Post a Comment

0 Comments