C Programming - Control Instructions - Discussion
Discussion Forum : Control Instructions - Point Out Errors (Q.No. 5)
5.
Which of the following errors would be reported by the compiler on compiling the program given below?
#include<stdio.h>
int main()
{
int a = 5;
switch(a)
{
case 1:
printf("First");
case 2:
printf("Second");
case 3 + 2:
printf("Third");
case 5:
printf("Final");
break;
}
return 0;
}
Answer: Option
Explanation:
Because, case 3 + 2: and case 5: have the same constant value 5.
Discussion:
18 comments Page 1 of 2.
Pradeep said:
1 year ago
In the switch case, no duplicate case will be allowed.
Am I right?
Am I right?
Ravi said:
4 years ago
Arithmetic operation is allowed in switch case or not?
i.e case (arithmetic operation).
i.e case (arithmetic operation).
Vishalakshi said:
7 years ago
Thank you @Amrita.
Amrita said:
8 years ago
@Mohit bansal.
I compiled it. Got the output as mentioned by you mohit.
Explanation:
y=x=10 means the values of x and y have been assigned as 10.
Now,
Z = x<10.
Means, value of z has been assigned as the same as x( here it is 10 from previous line).
But z is not <10. z is =10(since x is 10).
Thus the condition is false and so it returned a value 0. If the condition was true, then the compiler would return a value of 1 and the output would be x=10, y=10, z=1.
This is like a rule.
I compiled it. Got the output as mentioned by you mohit.
Explanation:
y=x=10 means the values of x and y have been assigned as 10.
Now,
Z = x<10.
Means, value of z has been assigned as the same as x( here it is 10 from previous line).
But z is not <10. z is =10(since x is 10).
Thus the condition is false and so it returned a value 0. If the condition was true, then the compiler would return a value of 1 and the output would be x=10, y=10, z=1.
This is like a rule.
Venky said:
8 years ago
I compiled it. And I got it error.
So the options are wrong. Also no duplicates should be present in switch.
So the options are wrong. Also no duplicates should be present in switch.
Mohit bansal said:
8 years ago
#include<stdio.h>
int main()
{
int x=3,y,z;
y=x=10;
z=x<10;
printf("x=%d y=%d z=%d\n",x,y,z);
return 0;
}
The output is x = 10, y = 10, z = 0.
Please explain?
int main()
{
int x=3,y,z;
y=x=10;
z=x<10;
printf("x=%d y=%d z=%d\n",x,y,z);
return 0;
}
The output is x = 10, y = 10, z = 0.
Please explain?
Dhruv said:
8 years ago
What will case 3/2: be evaluated as? Can anyone help?
Ranjith karthick said:
8 years ago
I have an doubt in switch usage of operator is allowed or not. Can any one explain?
Shree said:
9 years ago
It is needed because the absence of break statement will leads the execution to continue
the codes until it finds break.
For example:
#include<stdio.h>
int main()
{
switch(1)
{
case 1:
{ printf("1");
break;// if break statement is not included,then the output will 1 2. else the output will be 1.
}
case 2:
{ printf("2");
break;
}
}
}
the codes until it finds break.
For example:
#include<stdio.h>
int main()
{
switch(1)
{
case 1:
{ printf("1");
break;// if break statement is not included,then the output will 1 2. else the output will be 1.
}
case 2:
{ printf("2");
break;
}
}
}
Sri said:
9 years ago
I have a doubt for every case the break statement is needed or not. If it is not needed what will be the output? Can any one explain me?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers