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.
Related
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.
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.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Could anyone explain these undefined behaviors (i = i++ + ++i , i = i++, etc…)
#include<stdio.h>
void call(int,int,int);
int main(){
int a=10;
call(a,a++,++a);
return 0;
}
void call(int x,int y,int z){
printf("x=%d y=%d z=%d\n",x,y,z);
}
This code is giving me output of 12 11 12 when run it. Could someone explain exactly how this is happening?
The behaviour of your code is undefined since you're changing a twice between sequence points:
call(a,a++,++a);
The behaviour is undefined because changing a variable twice between two sequence points.
c99 standard : 5.1.2.3 Program execution
2
"Accessing a volatile object, modifying an object, modifying a file, or calling a function
that does any of those operations are all `side effects` which are changes in the state of
the `execution environment`. Evaluation of an expression may produce side effects. At
certain specified points in the execution sequence called `sequence points`, all side effects
of previous evaluations shall be complete and no side effects of subsequent evaluations
shall have taken place."
Here you are modifying a variable a twice between two sequence points.
Extended EDIT : If you know these concept already and thinking about that , comma operator is a sequence point so it should work as a well definedprogram.Then you are wrong , used here in function call is comma separator not comma operator
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 tried this simple C program in GCC
#include<stdio.h>
int main(){
int x=5;
printf("%d,%d,%d,%d,%d",++x,x++,x,x++,++x);
return 0;
}
and the output was 9,7,7,6,6
i traced it and assumed that it will print 6,6,7,7,9 but i found my assumption in reverse order, how come!
Because your program has undefined behaviour. There is no sequence point between the evaluations of function arguments, and it is undefined behaviour to mutate the same object more than once without intervening sequence point.
The program is simply ill-formed. It is not a valid C program.
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)