-1

My program is

int main() {
    int i = 10;
    int j = 20;
    switch (i++, j--) {
        case 10:
            printf("The number is 10");
            break;
        case 20:
            printf("The number is 20");
            break;
        default:
            printf("Input Invalid");
    }
    return 0;
}

and i am getting output The number is 20. I need to know how it works can u explain. Thanks.

herohuyongtao
  • 49,413
  • 29
  • 133
  • 174
  • possible duplicate of [What does the comma operator \`,\` do in C?](http://stackoverflow.com/questions/52550/what-does-the-comma-operator-do-in-c) – Mohit Jain Jul 09 '15 at 13:06

2 Answers2

3

You're using the comma operator here. 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).

Thus

switch (i++, j--) {...}

is equivalent to

i++;
switch (j--) {...}
herohuyongtao
  • 49,413
  • 29
  • 133
  • 174
0

I believe you are using the switch case improperly. You are accepting two variable as an argument,

switch(a++ , b--)
{
   case(a>b):
   case(b<a):
   case(a==b):
}

If you solely give one number and two arguments, the compiler doesn't know what to do with them.

This is my experience if you are using a c based language.

Zac Wimer
  • 89
  • 5