I am unable to understand how the output of the below code is "-3" ?
#include <stdio.h>
void main()
{
int a = -5;
int k = (a++, ++a);
printf("%d\n", k);
}
What is the concept behind this int k = (a++, ++a); statement in c or c++?
I am unable to understand how the output of the below code is "-3" ?
#include <stdio.h>
void main()
{
int a = -5;
int k = (a++, ++a);
printf("%d\n", k);
}
What is the concept behind this int k = (a++, ++a); statement in c or c++?
It works because of the , operator which creates a sequence point.
§5.19.1 (Comma operator)
The comma operator groups left-to-right. A pair of expressions separated by a comma is evaluated left-to-right; the left expression is a discarded value expression (Clause 5). Every value computation and side effect associated with the left expression is sequenced before every value computation and side effect associated with the right expression. The type and value of the result are the type and value of the right operand; the result is of the same value category as its right operand, and is a bit-field if its right operand is a glvalue and a bit-field. If the value of the right operand is a temporary (12.2), the result is that temporary.
Therefore:
a is initialized to -5. a++ executes, and modifies a to -4.++a executes, modifies a to -3, and returns -3 to k.This is not undefined behaviour.
In your code,
int k = (a++, ++a);
is making use of "comma operator". After a gets initialized to -5, what is does is basically,
a++, discard the result. side effect of post-++, a is now -4, , sequence point.++a, return the result. a is now -3 (pre-++), which gets assigned to k.Ref: From C11 standard, chapter 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 is perfectly well-defined in both C and C++.
The expressions separated by the comma are evaluated from left to right.
The value of the entire expression will be the value of the second expression.
So, to break down (a++, ++a), a++ is first evaluated (then a is -4) and the result (-5) discarded, then ++a is evaluated. That value (-3) is assigned to k.
Here is used an expression with the comma operator as an initializer
int k = (a++, ++a);
According to the C Standard (and the same is valid for C++ ) (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
Initially variable a was initialized with -5
int a = -5;
So after evaluation the first expression of the comma operator and due to the fact thet "there is a sequence point between its evaluation and that of the right operand" a will be equal to -4 (a++). The result of the whole expression with the comma operator will be the value after evaluation the right expression ++a. Its value is -3
Thus varaible k will be initialized with -3. The same value will have variable a.