In c, can a switch statement have 2 arguments? [duplicate] - c

This question already has answers here:
What does the comma operator , do?
(8 answers)
Closed 6 years ago.
int main()
{
switch(1,2)
{
case 1:printf("1");break;
case 2:printf("2");break;
default: printf("error");break;
}
}
Is this valid in c?
I thought it shouldn't be , but when I compiled it , it shows no error and produces output 2.

Yes, this is valid, because in this case, the , is a comma operator.
Quoting C11, chapter §6.5.17, Comma operator, (emphasis mine)
The left operand of a comma operator is evaluated as a void expression; there is a
sequence point between its evaluation and that of the right operand. Then the right
operand is evaluated; the result has its type and value.
This (evaluates and) discards the left operand and uses the value of the right (side) one. So, the above statement is basically the same as
switch(2)
Just to elaborate, it does not use two values, as you may have expected something like, switching on either 1 or 2.

Related

comma operator in while loop [duplicate]

This question already has answers here:
What does the comma operator , do?
(8 answers)
Closed 7 years ago.
Guys when I attended a C quiz online, I came across a statement while(s++,6); where s is initialized to zero. I don't know what the while loop will exactly do when there is a comma operator in between. When I ran it on my gcc compiler it ran with no output. But when I changed the while condition as while(1,s++) it returned s value as 1. Can anyone tell me what is happening at that while.
The comma operator evaluates the left side and then throws away the result. The while condition keeps looping for any value other than zero. The first will be an infinite loop; the second will increment s and then stop.
I suspect in this case the comma was a typo and they meant to type a less-than.
From C11 standard §6.5.17:
The left operand of a comma operator is evaluated as a void expression; there is a sequence point between its evaluation and that of the right operand. Then the right operand is evaluated; the result has its type and value.
This means that 1,s++ evaluates 1 (so nothing happens), then it evaluates s++ and returns the result of that expression only.
So that's expression is equivalent to while (s++). If the left-hand side of a comma expression doesn't have any side effects, like in your situation, then you can remove it.

How does the C compiler interpret the following code sequence [duplicate]

This question already has answers here:
What does the comma operator , do?
(8 answers)
Closed 7 years ago.
I have this C code line:
int a;
a = (1, 2, 3);
printf("%d", a);
Why the value 3 is printed? (the last one).
The comma operator evaluates all its "members" but returns the value of the last expression.From the C11 standard:
The left operand of a comma operator is evaluated as a void
expression; there is a sequence point between its evaluation and that
of the right operand. Then the right operand is evaluated; the result
has its type and value.

Comma-Separated return arguments in C function [duplicate]

This question already has answers here:
What does the comma operator , do?
(8 answers)
Closed 7 years ago.
While completing a C programming test, I was given a question regarding the expected output from a function which seem to return two values. It was structured as follows:
int multi_return_args(void)
{
return (44,66);
}
The question caught me by surprise and inherently thought that if possible the first argument would be passed to the caller.
But after compiling it, the result is 66 instead. After a quick search I couldn't find anything about structuring a return statement like this so was wondering if some could help me.
Why does it behave like this and why?
The comma operator evaluates a series of expressions. The value of the comma group is the value of the last element in the list.
In the example you show the leading constant expression 44 has no effect, but if the expression had a side effect, it would occur. For example,
return printf( "we're done" ), 66;
In this case, the program would print "we're done" and then return 66.
In your code,
return (44,66);
is making (mis)use of the comma operator property. Here, it essentially discards the first (left side) operand of the , operator and returns the value of that of the second one (right operand).
To quote the C11 standard, chapter §6.5.17, Comma operator
The left operand of a comma operator is evaluated as a void expression; there is a sequence point between its evaluation and that of the right operand. Then the right operand is evaluated; the result has its type and value.
In this case, it is same as writing
return 66;
However, FWIW, the left hand side operand is evaluated as a void expression, meaning, if there is any side effect of that evaluation, that will take place as usual, but the result of the whole expression of the statement involving the comma operator will be having the type and the value of the evaluation of the right hand side operand.
This will return 66. There is nothing special about returning (44,66).
(44,66) is an expression that has the value 66 and therefore your function will return 66.
Read more about the comma operator.
Coma operator always returns the value of the rightmost operand when multiple comma operators are used inside an expression.
It will obviously return 66.

What does comma operator in C do when we return an integer with two values? [duplicate]

This question already has answers here:
What does the comma operator , do?
(8 answers)
Closed 7 years ago.
I was actually returning a float value when I typed , instead of . but it did not give me any error. Then I tried running the below code.
#include<stdio.h>
#include<conio.h>
int getValue();
int main()
{
int a = getValue();
printf("%d", a);
return 0;
}
int getValue()
{
return 2, 3;
}
Now the output is 3, that is it returned the second value. This happened two years ago and was searching for the proper answer since then.
Studying answers to this question I came to know that it returns the second value after evaluating, but what does it do with the first one?
I studied the logic of the stack(how the values are pushed and pop internally) but I don't think this have anything to do with it.
Does it processes the two values or do something else?
Quoting C11 standard, chapter §6.5.17, Comma operator
The left operand of a comma operator is evaluated as a void expression; there is a sequence point between its evaluation and that of the right operand. Then the right operand is evaluated; the result has its type and value.
Now, to answer your question
what does it do with the first one?
we can say, the first operand (left hand operand) is evaluated and the result is discarded.
NOTE: I mentioned the result, not the effect.
Just to clarify, in your case, you won't notice the effect, but you can notice it's effect if both the left and right hand expressions are related to the same variable. For example, let's discuss a simple exmple
return p=3, p+2;
We can break down the return statement like
asign a value 3 to the variable p [left hand side operator of ,]
execute p+2, ehich generates a value of 5. [right hand side operator of ,]
return the value 5 as the value of second argument [following the clause : "the result (of , operator) has its (evaluation of right-operand) type and value."]
See a live demo.
comma operator will evaluate from left to right and return the right most expression or value. This is equivalent to:
int getValue()
{
(void)2;
return 3;
}

Can't understand the output of C progaram [duplicate]

This question already has answers here:
Using comma operator in c
(1 answer)
Not able to understand the reason for output
(3 answers)
Closed 8 years ago.
#include<stdio.h>
int main()
{
int x=10,y=12;
printf("%d",(x,y));
return 0;
}
The output of the program is 12. How?
The expression that you are evaluating is:
x,y
This expression uses the comma operator. The standard (6.5.17 Comma operator) says:
The left operand of a comma operator is evaluated as a void expression; there is a
sequence point between its evaluation and that of the right operand. Then the right
operand is evaluated; the result has its type and value.
So, in your code, x,y evaluates to y, which has a value of 12.
For a more expansive discussion, I refer you to cppreference.com. Although that discusses C++, the discussion for this operator is valid in the context of C. Particularly relevant to your question is this section:
The comma in various comma-separated lists, such as function argument lists (f(a, b, c)), initializer lists int a[] = {1,2,3}, or initialization statements (int i, j;) is not the comma operator. If the comma operator needs to be used in that context, it has to be parenthesized: f(a, (n++, n+b), c).
And that's exactly the situation in your question. If you had written:
printf("%d", x, y);
then there would have been no use of the comma operator, and you would have supplied one more argument to printf than format specifier.
You're by chance using the comma operator.
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).
That said,
printf("%d",(x,y));
is functionally equivalent to
printf("%d", y);
it is because first (x,y) is evaluated.
inside () the expression is x,y they are evaluated from left to right since the Associativity of Comma operator is left to right, so the last value of evaluating (x,y) is y.
read operator precedence and associativity rule and how expressions are evaluated under operator precedence to understand these type of expressions

Resources