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.
Prudhvi said:
1 decade ago
Please explain in detail.
Abhishek said:
1 decade ago
Is variable addition is allowed in case statements ?
RAJ said:
1 decade ago
Because case 3+2 is equal to case 5. So ambiguity occurs.
Hyma said:
1 decade ago
Yes addition is allowed in case statements but case 3+2 is equal to case 5 so ambiguity is occurs.
Knight said:
1 decade ago
Addition, multiplication, division are allowed in case statement but both the operand should be constant, operation is performed at compile time, for this case compiler get confused while choosing case 5 (ambiguity). So it will raise compile time error.
Deepu said:
1 decade ago
in C.
The cases cant be defined as the any operation methods!
Example: the following cases is not allowed.
case 3+2:
case 3/2:
case 3*2;
etc aren't allowed.
The definition after the case must a mere number or the character upon what data type you define.
example:
int a = 4;
switch (a)
case 1:
case 2:
case 3:
case 4:
or else it might be
char ch='A';
switch (ch)
case 'A':
case 'B':
etc..etc..etc.
The cases cant be defined as the any operation methods!
Example: the following cases is not allowed.
case 3+2:
case 3/2:
case 3*2;
etc aren't allowed.
The definition after the case must a mere number or the character upon what data type you define.
example:
int a = 4;
switch (a)
case 1:
case 2:
case 3:
case 4:
or else it might be
char ch='A';
switch (ch)
case 'A':
case 'B':
etc..etc..etc.
Naveen said:
1 decade ago
@Deepu.
you are wrong about those operation.
You can perform arithmetic operation on case expressions.
case 2+3:
case 3-2:
case 2*3:
case 3/2:
is allowed.
you are wrong about those operation.
You can perform arithmetic operation on case expressions.
case 2+3:
case 3-2:
case 2*3:
case 3/2:
is allowed.
Binal said:
1 decade ago
If there's no break statement used, case 5 will not matter right? So it should keep printing till break?
Sri said:
1 decade 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?
Shree said:
1 decade 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;
}
}
}
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers