Understand comma operator - c

int main()
{
int a = (1,2,3);
int b = (++a, ++a, ++a);
int c= (b++, b++, b++);
printf("%d %d %d", a,b,c);
}
I am beginner in programming. I am not getting how does this program shows me output of 6 9 8.

The , used in all the three declarations
int a = (1,2,3);
int b = (++a, ++a, ++a);
int c = (b++, b++, b++);
are comma operator. It evaluates the first operand1 and discard it, then evaluates the second operand and return its value. Therefore,
int a = ((1,2), 3); // a is initialized with 3.
int b = ((++a, ++a), ++a); // b is initialized with 4+1+1 = 6.
// a is 6 by the end of the statement
int c = ((b++, b++), b++); // c is initialized with 6+1+1 = 8
// b is 9 by the end of the statement.
1 Order of evaluation is guaranteed from left to right in case of comma operator.

The code is not in any way good and nobody in their right mind would ever write it. You should not spend any time in looking at that kind of code, but I will still give an explanation.
The comma operator , means "do the left one, discard any result, do the right one and return the result. Putting the parts in parentheses doesn't have any effect on the functionality.
Written more clearly the code would be:
int a, b, c;
a = 3; // 1 and 2 have no meaning whatsoever
a++;
a++;
a++;
b = a;
b++;
b++;
c = b;
b++;
The pre- and post-increment operators have a difference in how they act and that causes the difference in values of b and c.

I am beginner in programming. I am not getting how does this program
shows me output of
Just understand comma operators and prefix ,postfix .
according to rules mentioned in links given to you
int a = (1,2,3); // a is initialized with 3 last argument .
int b = (++a, ++a, ++a); // a is incremented three time so becomes 6 and b initilized with 6 .
int c = (b++, b++, b++); // b incremented two times becomes 8 and c initialized with 8.
// b incremented once more time becomes 9

Related

Evaluation order in ternary operator with increments

#define MAX(a,b) ((a)>(b) ? (a) : (b))
int main(void) {
int a=2;
int b=3;
int c = MAX(a++,b++); // c=((a++)>(b++) ? (a++) : (b++));
printf("\na= %d", a);// a=3
printf("\nb= %d", b);//b=5
printf("\nc= %d", c);//c=4
a=3;
b=2;
cc = MAX(a++,b++); // c=((a++)>(b++) ? (a++) : (b++));
printf("\na= %d", a); // a=5
printf("\nb= %d", b); //b=3
printf("\nc= %d", c); //c=4
return 0;
}
I would like to know why c is not evaluating to 5.
It appears to me the evaluation order should be:
First both a and be get incremented in (a++)>(b++)
If for example the first one is greater, the ternary operator in
c=((a++)>(b++) ? (a++) : (b++)), goes to (a++), so a
increments again.
The result of the ternary expression, which is a twice-increment,
should be assigned to c, then c should have the greater value
twice-incremented, that is 5. However, I am getting 4. I suspect the
greater value's second increment is happening at the end, but I can't
explain why, since the parentheses seem to indicate that the
assignment comes at the end.
Any idea?
The postfix ++ operator has a result and a side effect. The result of a++ is the value of a before the increment - given
int a = 1;
int x = a++;
the value of x will be 1 and the value of a will be 2. Note that the side effect of adding 1 to a does not have to be applied immediately after evaluation - it only has to be applied before the next sequence point.
So, looking at
((a++) > (b++)) ? (a++) : (b++)
The ?: operator forces left-to-right evaluation, so the first thing that happens is that (a++) > (b++) is evaluated1. Since a is initially 2 and b is initially 3, the result of the expression is false (0). The ? operator introduces a sequence point, so the side effects to a and b are applied and a is now 3 and b is now 4.
Since the result of the condition expression was 0, we evaluate b++. Same thing, the result of the expression is the current value of b (4), and that value gets assigned to c. The side effect to b is applied at some point, and by the time everything's finished, a is 3, b is 5, and c is 4.
Although within that expression either a++ or b++ may be evaluated first, since the > operator doesn't force left-to-right evaluation.
Let's consider for example this declaration
int c = MAX(a++,b++);
after the macro substitution there will be
int c = (( a++) >( b++ ) ? (a++) : (b++));
The variables a and b are initialized like
int a=2;
int b=3;
As a is less than b then the third expression (b++) will be evaluated as a result of the conditional operator. In the first expression ( a++) >( b++ ) a and b were incremented. There is a sequence point after the evaluation of the first expression.
So a will be set to 3, b will be set to 4.
As it was already said the value of the conditional operator is the value of the third expression (b++) where there is used the post-increment. The value of the post-increment operator is the value of its operand before incrementing.
From the C Standard (6.5.2.4 Postfix increment and decrement operators)
2 The result of the postfix ++ operator is the value of the
operand. As a side effect, the value of the operand object is
incremented (that is, the value 1 of the appropriate type is added to
it).
So the value of the conditional operator is 4. This value will be assigned to the variable c. But as a side effect the value of b will be incremented.
Thus after this declaration a will be equal to 3, b will be equal to 5 and c will be equal to 4.
For clarity this declaration
int c = (( a++) >( b++ ) ? (a++) : (b++));
in fact can be rewritten in the logically equivalent way.
int result = a > b;
++a;
++b;
int c;
if ( result != 0 )
{
c = a++;
}
else
{
c = b++;
}
For a++ it's equivalent as saying
int temp = a;
a = a + 1;
return temp;
so in the case of a=2, b=3, we can go to the condition (a++)>(b++) and we can have a rewritten as
int temp1 = a;
a = a + 1;
return temp1;
and b rewritten as
int temp2 = b;
b = b + 1;
return temp2;
now since it's only a conditional statement, we're really just evaluating the old values of a and b before the increment which is temp1 = 2 and temp2 = 3 while at the same time a and b values have changed a = 3, b = 4. Since temp1 < temp2 from the old value, we go to the false clause of the ternary operator (b++) and do the same thing as we did before
int temp3 = b;
b = b + 1;
return temp3;
so now b is 5 while the returned temp3 is the previous value of b which is 4. Hope this helps!

In C, is a+++b equal to a+b++? [duplicate]

This question already has answers here:
Why doesn't a+++++b work?
(9 answers)
Closed 5 years ago.
In C, is a+++b equal to a+b++?
They are and will be equal if you supply the same initial values of the operands.
In your case, the side effect of the first statement (post increment on a) is affecting the second one. Due to the presence of the post-increment in the first expression, a is incremented to 3 before the next statement is executed.
Re-initialize the variables with the same genesis value before calculating the second one.
You need to check the C operator precedence to understand it.
The confusing thing here is that a+++b may be read as either a + (++b) or as (a++) + b. According to the C operator precedence, it is actually looks like:
int a=2, b=3, c;
c = (a++) + b; // 2+3=5 and 'a' will be 3 after that line
printf("%d\n",c); // c = 5
c = a + (b++); // 3+3=6 and 'b' will be 4 after that line
printf("%d\n",c); // c= 6
From the link above:
++ as sufix has highest priority.
++ as prefix has lower priority.
+ has even lower priority.
int a=2, b=3, c;
c = (a++) + b; // The value for a will be 3 after that line
printf("%d\n",c); // c = 5
c = a + (b++); // So here a value is 3 (3+3) =6 after executing this line b value will be 4
printf("%d\n",c); // c= 6
To avoid this you need to reinitialize the variables
c = a+++b;
is equivalent
c = a++ + b;
a++ means post-increment, means expression takes the value of a and then increment.
c = a+b++;
is equivalent
c = a + b++;
b++ means post-increment, means expression takes the value of b and then increment.
If you provide same value in both cases, then both express variable c same.

what is the value of a++ + a if value of a is 5? [duplicate]

This question already has answers here:
Order of operations for pre-increment and post-increment in a function argument? [duplicate]
(4 answers)
Closed 7 years ago.
i am a beginner in c, and i am finding it difficult to understand the post and pre increment i have given my code below,i already compiled it in a turbo c++ compiler and i got output as
a = 6 and b = 10 but since the post increment operator is used the output should be a = 6 and b = 11 ,why is it not happening?could someone explain it..
#include<stdio.h>
int main()
{
int a=5,b;
b = a++ + a;
printf("\na = %d and b = %d",a,b);
return 0;
}
The behaviour of a++ + a; is undefined in C. This is because the + is not a sequencing point and you're essentially attempting to increment and read a in the same expression.
So you can't guarantee a particular answer.
In order to understand prefix and postfix increments, use statements like b = a++; and b = ++a;
What happens in the following?
b = a++ + a;
1) Is a incremented and its original value is then added to the original value of a?
2) Is a incremented and its original value is then added to the new value of a?
3) Is a on the right side fetched first and then added to the original value of an incremented a?
C allows any of theses approaches (and likely others) as this line of code lacks a sequence point which would define evaluation order. This lack of restriction allows compilers often to make optimized code. It comes at a cost as the approaches do not generate the same result when accessing a in the various ways above.
Therefore it is implementation defined behavior. Instead:
b = a++;
b = b + a;
or
b = a;
b = b + a++;
After int a = 5; the value of a is 5
b = a; // b is 5;
After int a = 5; the value of a++ is 5
b = a++; // b is 5
but the side effect of a++ is to increase the value of a. That increase can happen anytime between the last and next sequence points (basically the last and next semicolon).
So
/* ... */;
b = a++ + a;
#if 0
/* side-effect */ 5 + 6
5 /* side-effect */ + 6
5 + /* side effect mixed with reading the value originating a strange value */ BOOM
5 + 5 /* side effect */
#endif

why lvalue required as increment operand error? [duplicate]

This question already has answers here:
lvalue required as increment operand error
(4 answers)
Closed 8 years ago.
Why lvalue required as increment operand Error In a=b+(++c++); ?
Just Wanted to assign 'b+(c+1)' to 'a' and Increment 'C' by 2 at the same time.
I'M A Beginner Just Wanted A Clarification About What "LVALUE ERROR" Actually Is?
main()
{
int a=1,b=5,c=3;
a=b+(++c++);
printf("a=%d b= %d c= %d \n",a,b,c);
}
Postfix increment binds tighter than prefix increment so what you would want would be something like:
a = b + (++c)++;
This is not legal C, though, as the the result of prefix increment (like the result of postfix increment in your example) is not an lvalue. This means that it's just a value; it doesn't refer to a particular object like 'c' any more so trying to change it makes no sense. It would have no visible effect as no object would be updated.
Personally I think that doing it in two statements is clearer in any case.
a = b + c + 1;
c += 2;
LVALUE means, that there isn't a variable the operation is supposed to be performed on.
C files are basically nothing but text files, which require a particular formatting, so the compiler can understand it.
Writing something like ++Variable++ is complete nonsense for the compiler.
You can basically imagine ++c as:
Var += 1;
return Var;
while c++ is:
int Buf = Var;
Var += 1;
return Buf;
To 'repair' your code:
void main() {
int a=1,b=5,c=3;
a = b + (++c); //Equals 5 + 4
printf("a=%d b= %d c= %d \n",a,b, ++c); //a = 9, b = 5, c = 5
}
This way, you'll get the result you wanted, without the compiler complaining.
Please remember, that when using ++c or c++ in a combined operation, the order DOES matter.
When using ++c, the higher value will be used in the operation, when using c++, it will operate with the old value.
That means:
int a, c = 5;
a = 5 + ++c; //a = 11
while
int a, c = 5;
a = 5 + c++; //a = 10
Because in the latter case, c will only be '6' AFTER it is added to 5 and stored in a.

Why is the output different?

Please explain me why it behaves differently.
int main() {
int p;
p = (printf("stack"),printf("overflow"));
printf("%d",p);
return 0;
}
This gives the output as stackoverflow8. However , if I remove the paranthesis , then :
p = printf("stack"),printf("overflow"); gives the output as stackoverflow5
The Comma Operator
The comma operator has lower precedence than assignment (it has a lower precedence than any operator for that matter), so if you remove the parentheses the assignment takes place first and the result of the second expression is discarded. So...
int a = 10, b = 20;
int x = (a,b); // x == 20
int y = a,b; // y == 10
// equivalent (in terms of assignment) to
//int y = a;
Note that the third line will cause an error as it is interpreted as a re-declaration of b, i.e.:
int y = a;
int b;
I missed this at first, but it makes sense. It is no different than the initial declaration of a and b, and in this case the comma is not an operator, it is a separator.

Resources