Functioning of increment operator in this case [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 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)

Related

how does post increment works in c? [duplicate]

This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 5 years ago.
here is very simple c program:
int main()
{
int i = 22;
printf("%d %d %d",i,i++,i);
return 0;
}
The result is:
23 22 23
how exactly post increment is working here ?
You cannot test correctly in this context.
The order of parameter evaluation for function parameters is undefined, hence you will have platform-dependent results.
The way i++ works actually is by first returning the old value and later incrementing, but that is a bad test.
Judging by the result you got, i++ is evaluated and returns the pre- incremented value. Then, the value of i, the return value of i++, and the value of i are passed to the print function.
This isn't ever something that you should rely on, as you may get different answers on different compilers or even with different optimization settings. As in the other answer, the order of parameter evaluation is undefined behavior.

Are side effects of undefined behaviour in C defined? [duplicate]

This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 8 years ago.
C code related to the question:
#include <stdio.h>
int main(int argc, char **argv)
{
int k = 0;
double b = k++ + ++k + k--;
printf("%d", k);
return 0;
}
The value stored in b is undefined, but what about k?
The place where I found this:
http://www.sanfoundry.com/online-c-test-precedence-order-evaluation/ Question #10
--EDIT--
What I found so far:
The value stored in b is not used anywhere, so if storing something into b would be the only UB, this program would not depend on UB.
But I also found this part in C99 6.5.2:
"Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression."
And listed under J.2. Undefined behavior:
" The behavior is undefined .... ‘‘shall’’ or ‘‘shall not’’ requirement that appears outside of a constraint is violated"
But the actual question is not answered yet.
-- EDIT #2 --
Not that I'm trying to write one, but a ''A strictly conforming program'' according to the standard :
"shall not produce output dependent on any unspecified, undefined, or implementation-defined behavior"
So the orignal example was wrong, since it did depend on undefined behaviour, it would be undefined even if one would replace the line
double b = k++ + ++k + k--;
with the line
k++ + ++k + k--;
So right now I'm looking for a better presentation of what question is about.
As soon as we hit undefined behaviour, the compiler can do whatever it wants to - including formatting the disk if it has access rights. So nothing can be defined in this situation, including side effects.

Why does i=2+2*i++ give the wrong result? [duplicate]

This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 9 years ago.
I felt that such an expression should be invalid but I was able to compile it and got the answer 5.
At the end I felt that even if it does answer should be 4 not 5.
int main(void)
{
int i=1;
// how is the next line evaluated ie in what sequence??
i=2+2*i++;
printf("%d",i);
return 0;
}
The output I got was 5. I can not understand how it should give the value.
This is undefined behaviour, since i is modified more than once between sequence points. For instance, this compiler gives 4 as the answer, because it puts the increment after the assignment. Another reasonable answer is 6, if the increment is before the assignment. But, as you've found, the compiler is permitted to make the answer whatever it wants, including 5.
See here for more about sequence points and undefined behaviour.

Yet an other i = i++ [duplicate]

This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 9 years ago.
I have encountered a sorcery today.
http://codepad.org/VW2vTpWw
Language: C
Code:
#include <stdio.h>
main()
{
int i = 5;
i = i++;
printf ("%i", i);
}
Output:
6
How? Why?
This is supposed to be tricky code but the other way around. The negligent programmer would think that i = i++ is just simple increment but it's not. Yet it works like one here. It's supposed to be 5! Like in JavaScript.
What should be happening.
i gets the value of 5.
i++ returns 5.
i is post incremented by i++ (to 6).
i gets the value of 5 (returned by i++).
the value of i (5) gets printed.
Yet it is 6.
I haven't been able to find a description to this on SO, or the whole internet (just the other way around).
What is broken here?
Please explain.
It is undefined behavior to store more than once to an object without an intervening sequence point.
In particular, your step 3 and 4 have no defined ordering, the increment (and store) or the store could happen first.

How compiler traverse printf arguments and gives output? [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…)
int main()
{
int a=1;
printf("%d %d %d",a,a++,++a);
return 0;
}
The above code is giving output 3 2 3 why????
It actually undefined in c and c++.
Undefined : modifying a scalar value twice between sequence points, which is what your code is doing. f(i++, ++i) is undefined behaviour because it modifies i twice without an intervening sequence point.
A good list of definitions
This is undefined behaviour
a++, ++a is done in same sequence point and this is undefined behaviour.
From Undefined behavior and sequence points :
In the Standard in §5/4 says
Between the previous and next sequence point a scalar object shall have its stored value modified at most once by the evaluation of
an expression.
What does it mean?
Informally it means that between two sequence points a variable must not be modified more than once. In an expression statement, the next sequence point is usually at the terminating semicolon, and the previous sequence point is at the end of the previous statement. An expression may also contain intermediate sequence points.
The mechanics of pre- and postincrementing are described here : http://c-faq.com/expr/evalorder2.html . However, this expression is undefined as mentioned in the previous answers.

Resources