What does the comma operator mean? [duplicate] - c

This question already has answers here:
C comma operator
(4 answers)
What does the comma operator , do?
(8 answers)
Closed 5 years ago.
I've declared variables and their values before but I've never done this on a single line before.
If I write
A, B = 0.0, 2;
Does this mean
A = 0
and
B = 2?

This expression
A, B = 0.0, 2;
is an expression with the comma operator (here are two comma operators). It can be presented like
( A ), ( B = 0.0 ), ( 2 );
As result the variable B will get the value 0.0. The variable A will be unchanged.
From the C Standard (6.5.17 Comma operator)
2 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 the value of the above expression is 2 and the type is int. The value of the expression is not used. So the only its side effect is assigning the value 0.0 to the variable B.

Related

Precedence order for a complex expression [duplicate]

This question already has answers here:
Assignment of two values in parentheses in C
(3 answers)
Closed 2 years ago.
#include<stdio.h>
void main()
{
int num, var;
num = (var=15, var+=35);
printf("%d",num);
}
here num evaluates as 50. I want to verify
My reasoning :- inside (..), we first read from left to right to see all operands available, make an order of preference, then evaluate += = , (in order). Finally we have var=50, which gets assigned to num. Am I correct?
The expression
(var=15, var+=35)
uses the comma operator, which evaluates each subexpression from left to right. The comma introduces a sequence point, so the side effects of each subexpression are applied before the next subexpression is evaluated. The type and result of the expression is the type and result of the rightmost subexpression. It's equivalent to writing:
var = 15;
var += 35;
num = var;
Please note that the comma operator used above is not the same as the comma that separates arguments in a function call - function arguments are not guaranteed to be evaluated from left to right, and side effects are only guaranteed to be applied before the function is called, not after each argument is evaluated.

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

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.

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.

how does compiler work during assignment operations? [duplicate]

This question already has answers here:
How does the Comma Operator work
(9 answers)
Closed 8 years ago.
Why is a assigned the value 3? Does the compiler simply take the last value from the list?
int a;
a=(1,2,3);
printf("%d",a);
How does compiler parse this statement or how it works internally?
Comma in (1,2,3) is a comma operator. It is evaluated as
a = ( (1,2) ,3 );
Comma operator is left associative. The result/value of the expression (1,2,3) is the value of the right operand of comma operator.
As pointed out in the comments, it's because you're using the comma operator. That means the 1 and 2 are evaluated and discarded. The three is the only thing left to be assigned. Without the parenthesis it would most likely be assigned as 1.

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