I was given a question and was asked to give the output.
int main(void){
int x = 2;
switch(x){
case 1,2,1: printf(\"Case 1 is executed\
FYI, today one may use the GCC case ranges extension when it is appropriate (which is not often) for similar results (only ranges are supported, not arbitrary lists of values).
case 1 ... 5:
case 'A' ... 'Z':
is it a compiler error or not.
It will cause a compilation error (don't know about TURBO C++ but in modern compilers).
This is not the way switch statement works (invalid syntax in c/c++). You cannot reuse a case
value in switch
statement (see the link given by chris) The way you can do this;
switch(x){
case 1: case 2: case 3: printf("Case 1 is executed");
break;
default : printf("Default case us execyted");
}
is it a compiler error or not.
The code is invalid in both languages: the case
expression must be a constant expression, and a constant expression can't contain a comma operator. (In C, this is stated explicitly; in C++, you have to unpick the grammar to find that a constant-expression must be a conditional-expression, which can't contain a comma).
Even if you were allowed to use the comma operator here, the switch
statement would still be invalid since two cases would both have the same value, 1.
And if not why does the code run only on turbo C.
Because both languages have changed significantly since that prehistoric compiler was last updated. Don't use it if you want to learn variants of C or C++ from this century.
What does comma operator mean in a switch statement?
It means you have an old compiler.
Edit post (to show case range
example)
The first two examples (including your original code ) exhibit incorrect switch statement syntax (with explanations). The third code example shows how stacking case labels is done correctly:
In your code, the compiler should have flagged the first comma after case 1,
<-- here
#include <ansi_c.h>
int main(void){
int x = 2;
switch(x)
{
case 1,2,1: printf("Case 1 is executed");
break; //error flagged at first comma, and all comma after in case
case 2,3,1: printf("Case 2 is executed");
break;
default : printf("Default case is executed");
}
return 0;
}
And, even modified like this you should also get a duplicate label error:
#include <ansi_c.h>
int main(void){
int x = 2;
switch(x)
{
case 1:
case 2:
case 1: printf("Case 1 is executed"); //duplicate label 1 error. (and others below)
break;
case 2:
case 3:
case 1: printf("Case 2 is executed");
break;
default : printf("Default case is executed");
}
return 0;
}
This example is perfectly legal (C99, C11) and useful: i.e., there are no duplicate labels, and the syntax complies with correct switch usage by stacking unique labels to handle conditions where case 1: OR case 2: OR case 3:
should be handled the same way, (in the same block). And of course the same is true for cases 4, 5 and 6.
#include <ansi_c.h>
int main(void){
int x = 2;
switch(x)
{
case 1:
case 2:
case 3: printf("Case 1,2 or 3 is executed"); //duplicate label 1 error. (and others below)
break;
case 4:
case 5:
case 6: printf("Case 4,5 or 6 is executed");
break;
}
getchar();
return 0;
}
This last example is included just for completeness. It illustrates the case range
expression. Although gaining interest among C programmers, it is not yet part of C99 or C11, rather an extension of Sun (a flavor of unix) and GNU C compiler (et al):
...
switch(x)
{
case 'a' ... 'z': //note: spaces between all characters ('a') and ellipses are required
printf("lowercase alpha char detected");
break;
case 'A' ... 'B':
printf("uppercase alpha char detected");
break;
default: printf("Default case is executed");
}
...
The reason for the ambiguous results you are seeing from one compiler to another may be that Turbo C is really really old. The version you are using was likely implemented against a version of the C standards that is no longer current.
Consider changing to a current compiler. An inexpensive (free) alternative is MinGW. MinGW is a very well maintained, open source compiler. If you like using Integrated Development Environments (IDE), Code::Blocks is one option, also free, and as an option comes bundled with MinGW.
Regarding compatibility, search for Comparison to Other Compiler Suites in this link to read about MinGW extensions. MinGW extensions, while expanding capabilities, sometimes make code written using them non-portable with other current compilers. Recommend using caution when using them.
Turbo C uses comma operator on switch cases and takes the last value, for example case 1, 2, 3: will be compiled as case 3: case 2, 3, 1 as case 1: hence Turbo C will not give you any error. Where as other compilers will not allow you case 1, 2, 3: kind of statement itself.
But in your case even Turbo c will give error because case statements are something like this case 1, 2, 1: and case 3, 2, 1: which will be complied as case 1: and case 1: hence as per switch case rules you can have only 1 case with a value and you cannot repeat the case
I prefer to use gcc compiler rather Turbo C
You can not use twice the same 'case' value
This is not correct: case 1: case 2: case 1: printf("Case 1 is executed");
Tried to compile on VS2010 => (error C2196: case value '1' already used)