This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 9 years ago.
When I type my code like given below :
int a=10,b,c,d,e;
c= a++;
d = ++a;
e = ++a;
printf("value of c=%d, d =%d, e=%d",c,d,e);
it gives me an output like c =10 , d= 12, e=13
and when we add these values,i.e 10+12+13 becomes 35,
but when I code it like :
b = a++ + ++a + ++a;
printf("value of b=%d" ,b);
it gives me output 36.
Can somebody describe what's the process behind this code and why the output of codes are different?
Thank you!
Delicious Undefined Behaviour, the order of evaluation of the operands of + (and many others) is left to the implementation. So it's not even always 36 for the second case.
int a=10,b,c,d,e;
c= a++;
d = ++a;
e = ++a;
printf("value of c=%d, d =%d, e=%d",c,d,e);
In statement c = a++ , value of a is used first(which is 10) and then incremented to11.
Statement d = ++a first increment a (which is 12 now) and then use its value to print in printf() statement.
Same for e = ++a.
Your second snippet
b = a++ + ++a + ++a;
printf("value of b=%d" ,b);
results in Undefined Behavior(http://en.wikipedia.org/wiki/Undefined_behavior)
Difference between them is second expression has not ended with a++ and yet to add some other values thats why a++ is 11, not 10
Related
This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 1 year ago.
Why does
int a, b = 10;
b = b++;
printf ("%d", b);
output 10
while
int a, b = 10;
a = b++;
printf ("%d", b);
outputs 11
how does this work? Why doesn't 'b' increment in first case?
b++ performs assignment with not-incremented value while ++b would do assignment with the incremented value. You only need b++; in your code without assignment to increment. As someone also pointed out b = b++; and b = ++b; are not good segments of code and you are at the mercy of the compiler.
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.
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
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
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.