How values are evaluated in this expression in GCC [duplicate] - c

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Could anyone explain these undefined behaviors (i = i++ + ++i , i = i++, etc…)
I have an expression in a program, initial value of i = 10
int j = i++ + i++;
it sets j as 20
but
int j = i++ + ++i;
it sets j as 22
Why is there a difference of two between statements? I think, difference should be of 1.
I know this is undefined in C, but why GCC is doing such things?

For i++ it is like: first do the equation then increment i.
For ++ i is like: first increment i then do the equation.
so i guess i++ + ++i is interpreted as i + (++(++i)) so thats the result is 22.
Wired things happen with wired syntax just dont do stuff like this ;).
If i'm right ++i + ++i would be 22 also.

Related

Does ++i perform the same task as i++ +1? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Say I'm assigning codes to ASCII chars, where code 1 is assigned to the ASCII value 0, the NUL char, and so on. I'm doing this iteratively, currently with i+++1, that is:
for (int i = 0; i < 256; i++) {
// assign code i++ +1
arr[i].code = i++ +1;
}
If I were to use ++i instead, would that suffice?
This might help
y = i+++x -> Add x to current value of i and assign the result to y and then increment value of i
y = ++1 -> increment i by 1 and assign the result to y
I would prefer to write that i++ + 1. But yes, ++i has the same effect and value asi+++1.
The latter logically does more work to arrive at the result, though: it keeps the old value ofi, increments i, adds one to the old value and yields the sum as result. The preincrement operator just increments i and uses its new value as result.
Technically both should compile to basically the same code given an optimising compiler.
Yes. They are the same.
++ bounds tighter than + so the expression is evaluated from left to right as
s = (i++) + 1
which can further be expanded as
s = i + 1
i = i + 1
On the other hand, s = ++i does the same task. It can be expanded as
i = i + 1
s = i
It is preferred that you use the second version because it is more readable and extensible. But I don't think there will be a difference in speed.
Yes, i+++1 and ++i will both evaluate to the value of i plus 1, and will both leave i incremented by 1.
It's an odd way to do it, is there something wrong with just doing:
for (int i = 1; i < 256; i += 2)
Anyone reading your code later would prefer the latter, I believe.

pre-increment and post-increment in C [duplicate]

This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 6 years ago.
First of all, I would like to apologize, if this question has been asked before in this forum. I searched, but could't find any similar problem.
I am a beginner in C. I was going through a tutorial and came across a code, the solution of which I can't understand.
Here is the code -
#include <stdio.h>
#define PRODUCT(x) (x*x)
int main()
{
int i=3, j, k;
j = PRODUCT(i++);
k = PRODUCT(++i);
return 1;
}
I tried running the code through compiler and got the solution as "j = 12" and "k = 49".
I know how #define works. It replace every occurrence of PRODUCT(x) by (x*x), but what I can't grasp is how j and k got the values 12 and 49, respectively.
Any help would be appreciated.
Thank you for your time.
Your code will invoke undefined behavior. Anything could happen. The macro in statements
j = PRODUCT(i++);
k = PRODUCT(++i);
will be expanded to
j = x++ * x++;
k = ++x * ++x;
In both statements x is being modified more than once between two sequence points.

confusion with the output of the program [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Could anyone explain these undefined behaviors (i = i++ + ++i , i = i++, etc…)
Undefined Behavior and Sequence Points
#include<stdio.h>
int main(){
int i=5,j=5,y,x;
int m=++i;
int n=++i;
x=m+n;
y=++j + ++j ;
printf("%d %d ",x,y);
return 0;
}
OUTPUT : 13 14
Can any one plz explain why 'y' value is 14 and not 13.
Most compilers will increment j twice before performing the addition and attributing the value to y, that is why the result in your case is 14.
The C standard doesn't specify the order of evaluation of that expression, though, so on another compiler the result could be 13 indeed.
In other words, this is undefined behavior and should be not be used other than in obfuscation contests and puzzles.

Functioning of increment operator in this case [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Could anyone explain these undefined behaviors (i = i++ + ++i , i = i++, etc…)
I have this code but i'm not getting how its functioning.
main()
{
int i=1;
i= ++i + ++i + ++i;
printf("%d",i);
}
I tried to compile it and im getting the output 10 but i've no idea how.
What I figured out is the two ++i are getting assigned the value 3 and the one ++i is getting the value 4, before the addition operation is performed. I cant figure out how increment operator is working in this case. Plz help me out with this.
The behavior is undefined .. there are a lot of posts similar to this if you search on SO.
For instance What would the evaluation order of x = x++ + ++x; be? or Why are these constructs (using ++) undefined behavior? and more.
Finally, just an opinion/comment: I don't think anyone would advocate writing that type of code as it's also hard to understand (hence the reason for your question).
I'm pretty sure you're not supposed to do that. Basically, don't modify a value more than once inside of the same expression. To do otherwise invokes "undefined behavior", which is a fancy way of saying "the compiler makes no guarantees of what will happen.
(Technically, the rule is don't modify a value more than once between the same sequence points)

What's exactly difference between i++ and ++i in C? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
(C) What is the difference between ++i and i++
int a[3][4]={1,2,3,4,5,6,7,8,9,10,11,12};
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
printf(“%-4d”,a[i][j]);
}
printf(“\n”);
}
In the above case, you won't recognize it.
But it's essentially the following:
int x = a[i++] first reads from à afterwards, increases i
int x = a[++i] first increases i, afterwards reads from a
++i Increments $i by one, then returns $i.
i++ Returns $i, then increments $i by one.
i++ is a post-increment operator ie., the current value of i is used for the operation and the value is incremented by 1 after the operation.
++i is pr-increment operator ie., the value of i is incremented and the new value of is used in the operation.
int i = 2;
int a = ++i; // a is 3, i is 3
int b = i++; // b is 3, i is 4
++i is a pre-increment, while i++ is a post-increment.
In that situation, because the type is an int and it happens in a for-loop, nothing; there is no performance benefit to either.

Resources