3
int main(){
char c='a';
switch(c){
    case 'a' && 1: printf("Hello");
    case 'b' && 1: printf("hey");
                   break;
         default : printf("Goodbye");
          }
}

When I compile this code the result was "compilation error", which (according to me) is because internally both the expression is true and hence for whatever character we take for "c",the constant expression inside for both cases will always be true.

But now comes the doubt that I am not able to understand, how code in interpreted internally and how compiler actually interprets this code?

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
yash
  • 77
  • 3
  • 10
  • I received this code as a question by friend, what i actually expected was "as 'a' is non-zero value (97) and &&(logical) 1, hence the expression will be TRUE, hence the statement inside case will be executed, also because there is no break statement for first case control will shift to case second and the code inside "case 'b' && 1:" will also be executed. – yash Jun 02 '22 at 13:09
  • Expected output : Hellohey – yash Jun 02 '22 at 13:10
  • The absence of a `break` statement after `printf("Hello");` is irrelevant here. – Jabberwocky Jun 02 '22 at 13:10
  • 1
    `switch` statements do not go looking for case expressions that are “true”. They are not sequences of implied `if` statements, executing a case “if” is expression is true. A `switch` statement takes a value (the `c` in `switch(c)`) and sends program control to the `case` label that has the same value (if there is one). Each `case` label should be a constant value that could match the `switch` value; a `case` label should not be a “true/false” expression (except when dispatching based on a true/false value, which would be weird). – Eric Postpischil Jun 02 '22 at 13:12

3 Answers3

5

Both expressions 'a' && 1 and 'b' && 1 have the value 1.

Therefore your code is strictly equivalent to this:

...
switch(c){
    case 1: printf("Hello");
    case 1: printf("hey");
                   break;
         default : printf("Goodbye");
          }
}
...

Hence the error message because two or more case labels cannot have the same value. The C language does not allow this.

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
  • Okay got it, i thought since the constant expression inside case results in 1 we can just continue with the code inside case statement. – yash Jun 02 '22 at 13:14
  • @ AndreasWenzel, just did, sorry i am relatively new on website still not very fond with functionality, thank you for informing! – yash Jun 02 '22 at 13:28
3

The case expressions 'a' && 1 and 'b' && 1 both evaluate to 1 because in both cases each operand is non-zero.

This means you have two cases with the same value, which is not allowed.

dbush
  • 205,898
  • 23
  • 218
  • 273
3

For starters according to the C Standard (6.8.4.2 The switch statement)

3 The expression of each case label shall be an integer constant expression and no two of the case constant expressions in the same switch statement shall have the same value after conversion

In this case labels

case 'a' && 1: printf("Hello");
case 'b' && 1: printf("hey");

there is used the logical AND operator. According to the C Standard (6.5.13 Logical AND operator)

3 The && operator shall yield 1 if both of its operands compare unequal to 0; otherwise, it yields 0. The result has type int.

As the character integer literals 'a' and 'b' and the integer literal 1 are not equal to 0 then the both expressions evaluate to the same integer value 1 at compile time.

So in fact you have

case 1: printf("Hello");
case 1: printf("hey");

As a result the two case labels have the same constant integer expression in the same switch statement. So the compiler issues an error.

Using the logical AND operator containing the operand equal to 1

case 'a' && 1: printf("Hello");
case 'b' && 1: printf("hey");

does not make a sense.

Maybe you mean the following labels

case 'a' & 1: printf("Hello");
case 'b' & 1: printf("hey");

that is labels with the bitwise AND operator.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Actually, i received this code as a question by friend, what i actually expected was, as 'a' is non-zero value (97) and &&(logical) 1, hence the expression will be TRUE, hence the statement inside case will be executed, also because there is no break statement for first case control will shift to case second and the code inside "case 'b' && 1:" will also be executed. – yash Jun 02 '22 at 15:19
  • Expected output : Hellohey – yash Jun 02 '22 at 15:19
  • @trev And do you have obtained the expected output after changing the case labels as I showed? – Vlad from Moscow Jun 02 '22 at 15:22
  • Yeah it did thank you very much, was it because, now that we have applied the Bitwise&, now the result will solely depend on character "a" and "b" and any other character in that sense, right? – yash Jun 02 '22 at 18:26
  • @trev In fact it is a test whether the internal code of a character is odd or even.:) – Vlad from Moscow Jun 02 '22 at 18:28
  • thank you very much, you have been big help! – yash Jun 02 '22 at 18:29