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 ..
Related
This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 1 year ago.
I'm not able to understand, why this code produces this output. Can anyone help me with this??
#include <stdio.h>
int main( void )
{
int num = 5;
printf( "%d %d %d", num, ++num, num++ );
}
The output will be 7 7 5 (and NOT 5 6 7)
Making side effects happen in the same order as in some other compiler.
It is never safe to depend on the order of evaluation of side effects. For example, a function call like this may very well behave differently from one compiler to another:
void func (int, int);
int i = 2;
func (i++, i++);
There is no guarantee (in either the C or the C++ standard language definitions) that the increments will be evaluated in any particular order. Either increment might happen first. func might get the arguments ‘2, 3’, or it might get ‘3, 2’, or even ‘2, 2’.
https://gcc.gnu.org/onlinedocs/gcc/Non-bugs.html
This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 4 years ago.
I think the best way to describe the problem is to actually show the output of this simple code:
Image contains code and output
#include<stdio.h>
int main()
{
int a=5,b;
b=a-++a;
printf("%d %d",a,b);
return 0;
}
As you can see, the values returned are logically correct.
++a increases the value of a to 6. Then b=6-6=0.
However, when I take the value of 'a' as user input using scanf, the following happens:
Image contains code and output
#include<stdio.h>
int main()
{
int a,b;
scanf("%d",&a);
b=a-++a;
printf("%d %d",a,b);
return 0;
}
Shouldn't the results be identical or am I missing something simple here?
The form a - ++a has an undefined value because the order of executions of the argument of '-' is undefined.
In the first case the compiler knows the value of a so it can optimize the code and finally all is known at compile time, not in the second case.
The fact the value is not the same when computed at compile time or execution time is a consequence of the form a - ++a whose result is undefined.
This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 9 years ago.
For the testing code as follow:
#include <stdio.h>
int addTen(int x, int b[])
{
b[2] = x + b[2];
return b[2];
}
void main(void)
{
int a[3] = {4,5,6};
int i = 2;
printf("%i %i %i \n", a[i], addTen(10,a), a[i]);
}
Why is the output is 16, 16, 6? I know that even if the compiler processes the order from right to left like a[i] <- addTen(10,a) <-a[i]. After calling addTen(10,a), a[i] is already 16 (not 6). So why the output is not 16, 16,16? THANKS!
It's undefined behavior, you should read about sequence points. You're modifying a and simultaneously reading it in a same expression.
In addition the order of evaluating is not defined.
There's no order defined for evaluating arguments. The compiler is free to evaluate arguments in any order, and will normally choose the most convenient order. So, you can't define any expected output.
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.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Could anyone explain these undefined behaviors (i = i++ + ++i , i = i++, etc…)
Code 1:
main()
{
int a=5;
printf("Value : %d %d %d %d\n",a++,a++,++a,a++);
}
ANS:
Value : 8 7 9 5
Code 2:
main()
{
volatile int a=5;
printf("Value : %d %d %d %d\n",a++,a++,++a,a++);
}
ANS:
Value : 8 7 7 5
Code 3:
main()
{
int a;
scanf("%d",&a);
printf("Value : %d %d %d %d\n",a++,a++,++a,a++);
}
INPUT is 5
ANS:
Value : 8 7 7 5
How the above programs are getting different outputs ?
I experimented volatile variable , it is used to prevent compiler optimizations.
So I understood the Code 2. But I want to know that ,how the Code 1 and 3 are working ?
volatile is to tell the compiler to read the value from memory every time as opposed to do any caching of values as part of its optimizations.
Your code exhibits undefined behaviour which is not going to be changed in anyway by using volatile.
What you have is an Undefined Behavior.
Your code modifies a variable more than once without an intervening sequence point.
You cannot use this code to draw any conclusions. It assures only one thing: that the output is Undefined.
This answer of mine explains in detail why this is undefined behavior. Note that though the answer is for C++, exact same reasoning and specifications exist for C as well.
what is the difference between volatile and local variable in c?
Using the keyword volatile tells the compiler to restrain from any optimizations by use of register caches.
Good Read:
Herb Sutter's excellent artile "volatile vs. volatile"
It's undefined behavior. No one can predict the correct output of the
printf("%d %d",a++,++a);
which one is executed first whether a++ or ++a so it's different output for different compiler or different scenarios.