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.
Related
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
This question already has answers here:
Why doesn't a+++++b work?
(9 answers)
Closed 8 years ago.
The following code prints 7. It increments value of a, but b remains unaffected. Post increment operator is not supposed to increment first and then use the value which it has done in this case. It should be the other way around. Why this change in behaviour? Also, and perhaps more importantly, why is b not pre-incremented?
int main() {
int a=5, b=2;
printf("%d", a+++b);
}
You have a well defined behavior. Where
a++ + b = 5 +2 = 7
If you have a pre-incrementor operator like
printf("%d\n",++a+b);
Then the output would be 8
The ++ operator has higher precendence than the + unary plus operator so the evaluation from left to right of
a+++b will happen
a++ + b which will fetch you 7
Post-incrementation is done by taking the value of your variable first which in this case is 5 so
a= 5 and b=2
In your code a is added to b and then it is incremented
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
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.
This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 3 years ago.
I am new to C, i have an Increment operator program in C
#include<stdio.h>
main(){
int a, b;
a = 2;
b = a + ++a + ++a;
printf("%d", b);
getchar();
}
The output is 10, can someone explain me how the output will be 10 .
a + ++a + ++a;
Behaviour for this is undefined.
The compiler might generated code that evaluated this as 2 + 4 + 4 or 3 + 3 + 4, but any combination/ordering of incrementing and accessing is a "valid" result.
This is undefined, the ++i can happen in any order.
Function call arguments are also ambigiously evaluated, e.g. foo(++i,++i).
Not all operator chains are undefined, a||b||c is guaranteed to be left-to-right, for example.
The guarantees are made in places known as sequence points although this terminology is being deprecated and clarified in C++0x.
What's odd in your example is that neigher 2+3+4 nor 4+4+3 happened, so the compiler evaluated the left side first in one step and the right side first in the other. This was probably an optimisation to flatten the depencency graph.