Strange behavior of printf and i++ [duplicate] - c

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 !

Related

Why does this program print 25 instead of 27 if I input 5? [duplicate]

This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 7 years ago.
int main(void)
{
int i;
scanf( "%d", &i );
i = i++ * i++ ;
printf( "%d", i );
getchar();
getchar();
return 0;
}
Why does this program print 25 instead of 27 if I input 5 ?
i = i++ * i++ ;
is undefined behaviour and so can do whatever it pleases, such as return 25, return 27, return 30 (which probably makes more sense than 27), format your hard disk, or even laugh derisively at you.
:-)
The C standard (both C99 and C11, and possibly earlier though I haven't checked) has things known as sequence points, and you are not permitted to change the same variable twice without an intervening sequence point (of which the multiplication symbol * was not one).
You can see what are considered sequence points in Appendix C of both those iterations of the standard.

Explain the output? [duplicate]

This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 8 years ago.
#include<stdio.h>
int main()
{
int a = 10;
printf("%d %d %d",++a,a++,a--);
return 0;
}
I edited the code a bit..now the outputs are : 11 9 10
It's more complex now..
It's up to the compiler in which order he evaluates the parameters of a function call.
If the compiler goes from left to right (that would explain your output):
a is 10
prä-increment which means a is incremented (the value 11 is passed as parameter)
post-decrement which means a is decremented later (the the value 11 is passed as parameter)
post-increment which means a is incremented later (the value 10 is passed as parameter)
But if I compile this e.g. with another compiler I could get different output.
Rewriting it as follows may make it easier to understand:
NOTE: I have made the assumption that the compiler will produce code to evaluate the parameters from left to right! This may be compiler specific.
#include<stdio.h>
int main()
{
int a = 10;
int param2, param3, param4;
param2 = ++a; // increments a to 11 and evaluates to 11
param3 = a--; // evaluates to current value of a then decrements a (11)
param4 = a++; // evaluates to current value of a then increments a (10)
printf("%d %d %d",param2,param3,param4);
return 0;
}
The place of increment(++) and decrement(--) operator is very important. So in case of ++a the value is incremented from 10 to 11 and then printed, for a-- the current value is printed i.e. 10 and then the a is incremented to 11. Similarly in last case a++ current value 11 is printed and it is incremented to 12.

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.

Post increment in C code giving unexpected answer [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…)
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).

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