C Programming - Control Instructions - Discussion
Discussion Forum : Control Instructions - Point Out Errors (Q.No. 3)
3.
Point out the error, if any in the program.
#include<stdio.h>
int main()
{
int i = 1;
switch(i)
{
printf("This is c program.");
case 1:
printf("Case1");
break;
case 2:
printf("Case2");
break;
}
return 0;
}
Answer: Option
Explanation:
switch(i) becomes switch(1), then the case 1: block is get executed. Hence it prints "Case1".
printf("This is c program."); is ignored by the compiler.
Hence there is no error and prints "Case1".
Discussion:
8 comments Page 1 of 1.
SwapnilBais said:
9 years ago
The printf is ignored because the compiler after reaching to the switch statement it directly jumps on the cases which is called by the user.
(2)
Nijara said:
9 years ago
Why is the printf ignored by the compiler?
(1)
Rashmi said:
5 years ago
Switch statement does not print any statement outside case and default blocks.
(1)
Rama said:
1 decade ago
What is the use of int main () and return 0? Can't we write main () without any return?
Jay said:
1 decade ago
Warning shall be there: unreachable code(for printf statement)....(according to turbo C++ compiler).
Aman said:
1 decade ago
@Rama:
If we used datatype along with fun name so it is necessary to return something value if you wish to write main with return you can use void infront of main.
If we used datatype along with fun name so it is necessary to return something value if you wish to write main with return you can use void infront of main.
Pooja said:
9 years ago
But here, there are two cases so don't anyone think I should be initialized with 2!?
Swapnil Bais said:
9 years ago
@Nijara.
printf is ignored because the compiler after coming to the switch statement it directly jumps to the case.
printf is ignored because the compiler after coming to the switch statement it directly jumps to the case.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers