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.
Related
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.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Could anyone explain these undefined behaviors (i = i++ + ++i , i = i++, etc…)
There is a code following below and i ma facing a very serious problem in understanding the logic for the code.
#include <stdio.h>
#include <stdlib.h>
int main(void )
{
int i = 1 ;
printf("\n%d %d %d %d\n",++i,i++,i++,++i) ;
return 0 ;
}
I am using gcc compiler under the linux distro named Mandriva. In the above mentioned i have used pre and post increment with a variable in the printf statement.
The output that i am supposed to get is 2 2 3 5, but i am getting a different output.
Please help me in this code.
I am feeling much difficult in this code.
It's undefined behavior. There's no sequence points between the increments of i.
Any result is a correct result (including your hard drive being formatted).
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Undefined Behavior and Sequence Points
the output of the programme
#include<stdio.h>
main()
{
int i = 10;
printf("%d %d %d\n", ++i, i++, ++i);
}
is 13 11 13. Can someone please explain this ?
It's the oldest question ever. Why do people find this so fascinating?
This is undefined behavior; you're relying on side-effects without a sequence point between modifications.
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).
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Could anyone explain these undefined behaviors (i = i++ + ++i , i = i++, etc…)
int main()
{
int a=5,s;
s=++a + ++a;
printf("%d",a);
printf("%d",s);
}
output is 7 and 14
BUT
int main()
{
int a, s;
printf("Enter value of a");
scanf ("%d",&a);
s=++a + ++a;
printf("%d",a);
printf("%d",s);
}
input user gives is 5
output is 7 and 13
WHY?
Undefined behaviour:
s=++a + ++a;
Anything can happen when undefined, so your behaviour is perfectly valid.
I'd suspect this is an artifact of compiler optimisation, in the first example a is known so the compiler optimises the preincrements to occur before the addition. In the second example the value is unknown and the compiler does not optimise the sequence causing it to complete left to right. This may be a function of your specific compiler and it would need to be looked at specifically.
Undefined behaviour. Change it, or you risk being attacked by raptors.
hi budy this coding working correctly in VI compiler ..