Post increment in C code giving unexpected answer [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…)
Please explain the reason for the following outputs.
#include <stdio.h>
int main()
{
int i = 0, j = 0;
int val1 = 0;
int val2 = 0;
val1 = i+++i+++i++ ;
val2 = ++j+++j+++j ;
printf("value = %d\n", val1);
printf("value = %d\n", val2);
return 0;
}
Output :
value = 0
value = 7

You are modifying the same variable more than once without an intervening sequence point, this is Undefined Behavior.
An Undefined behavior simply means that there may or may not be any feasible explanation to the behavior of the program.
Good Read:
Undefined behavior and sequence points

Multiple changes of variables without an intervening sequence point is undefined behaviour.
This means that there is no definition in the specification for what should happen. The compiler is allowed freely to do whatever it wants -- anything at all.
Sequence points are only present at ;, &&, ||, ? and : in the ternary operator, and , (the comma operator, not to be confused with the comma separating arguments in a function call).

Related

How does it works? Sequence of computing variable into printf [duplicate]

This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 2 years ago.
stumbled upon such a puzzle:
What will be shown on the screen?
#include <stdio.h>
void main()
{
int x = 10;
printf("x = %d, y = %d", x--, x++);
}
Curiously enough, but shown at the screen this: x = 11, y = 10;
But how??
Argument Evaluation Order is undefined in both C and C++. It's important to avoid any code that passes expressions dependent on each other that has to be evaluated before the function is called. It's a strict no.
int f1() { printf("F1") ; return 1;}
int f2() { printf("F2" ) ; return 1;}
printf("%d%d", f1(), f2()) ;
You can check out by adding several functions that contain a print statement and pass it to a function to observe this in action. You don't know what's coming, the C standard doesn't specify it, it depends on what compiler you use and how it optimizes your code.

Strange behavior of printf and i++ [duplicate]

This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 8 years ago.
I always forget which of i++ and ++i return which value. To test this I wrote the fallowing code:
int i;
i = 6;
printf ("i = %d, i++ = %d\n",i, i++);
printf ("i = %d, ++i = %d\n",i, ++i);
The resulting (unexpected and strange) output is:
i = 7, i++ = 6
i = 8, ++i = 8
But when I break down the printfs to 4 separate commands, I get the expected result:
printf ("i = %d, ",i);
printf ("i++ = %d\n",i++);
printf ("i = %d, ",i);
printf ("++i = %d\n",++i);
gives:
i = 6, i++ = 6
i = 7, ++i = 8
Why does this happens?
You have both unspecified and undefined behaviour:
Unspecified behaviour: you don't know the order of evaluation of the parameters in the printf call.
(The C standard does not specify this: it's up to the compiler and it's free to choose a way that best matches the machine architecture).
Undefined behaviour: The commas in the function call are not sequencing points. The behaviour is undefined as you're attempting to read and modify the same object without an intervening sequence point.
Because in you first block i is 6 and after the printf statement it gets incremented because it's postfix!
After that in the second printf statement it's prefix and gets executed before it gets used so 7 + 1 = 8.
In your second code block i is 6 in the first printf statement, because it's postfix! That's why the second output is 7 and the last printf statement has a prefix increment so it gets incremented before it gets used and is 8!
Prefix/ Postfix:
i++ //gets incremented AFTER it gets used (e.g. in a operation)
++i //gets incremented BEFORE it gets used (e.g. in a operation)
EDIT:
The evaluation order of the arguments of a printf statement is unspecifyed !

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.

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.

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.

Resources