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"); break; case 2,3,1: printf("Case 2 is executed"); break; default : printf("Default case us executed"); } return 0; }
The above code gives output as "Case 1 is executed" in Turbo C, But on codeblocks and compile online, it gives a compiler error.
Which one is correct? Is it a compiler error or not? And if not, why does the code run only on Turbo C?
The comma operator ( , ) evaluates each of its operands (from left to right) and returns the value of the last operand. This lets you create a compound expression in which multiple expressions are evaluated, with the compound expression's final value being the value of the rightmost of its member expressions.
TypeScript static code analysis: Comma and logical OR operators should not be used in switch cases.
In the C and C++ programming languages, the comma operator (represented by the token , ) is a binary operator that evaluates its first operand and discards the result, and then evaluates the second operand and returns this value (and type); there is a sequence point between these evaluations.
The comma operator (represented by the token, ) is a binary operator that evaluates its first operand and discards the result, it then evaluates the second operand and returns this value (and type).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With