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.
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:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 7 years ago.
#include <stdio.h>
int main ()
{
int a=10;
printf("%d %d %d",a,a++,a);
return 0;
}
The output I am getting is "11 10 11".
I thought the output would be "10 10 11".
why a is incrementing like this?
Because there is no guarantee about the order in which a C compiler evaluates the arguments. The only thing guaranteed (by the standard) is that they are all evaluated before doing the call. Therefore, you should never count on the order of evaluation of the arguments. Just consider it as random.
Hence, in general, avoid using auto-increment if the same variable exists more than once in an argument list.
This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 7 years ago.
I am executing this c program on gcc compiler and getting strange results.
So how is it possible
code:
#include<stdio.h>
int main()
{
int i;
i =10;
printf(" %d %d %d ",i++,i++,i); //output : 11 10 12
return 0;
}
as per me result should be 10 11 12 but I am getting 11 10 12.
How is it possible?
In C++, the order of evaluation of function arguments is undefined so if you use the increment operator multiple times in the argument to a particular function there is no 'correct' answer, they may be evaluated in any arbitrary order.
Please familiarize yourself with the concept of Sequence points. Only at such defined sequence points is guaranteed that all side effects of previous evaluations are performed. There are no sequence points between the list of arguments of a function. So, it leads to undefined behavior.
This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Sequence points when calling functions in C and undefined/unspecified behaviour
(2 answers)
Closed 9 years ago.
I am decrementing an integer while passing it to a function. But am seeing some unexpected behavior. I'm new to C, and not sure what to make of it:
#include <stdio.h>
void func(int, int);
int main(){
int i=3;
//func(i--, i); //Prints a=3, b=2
func(i, i--); //Prints a=2, b=3 ??
func(i, --i); //Prints a=2, b=2 ??
}
void func(int a, int b){
printf("a=%d\n", a);
printf("b=%d\n", b);
}
The first call to func works as expected, but what is the deal with the second and third calls?
The order of calculation of function arguments is not specified. E.g. GCC likes to calculate the argument values from right to left. When you have an operator that modifies a variable (or have any other side effect), there must be a sequence point between that operator and any other expression that uses that variable.
This is undefined bahavior, you can't expect either result.
Since parameters passes in a function from right, so in first call where you used post increment operator- it passes the same value (3) and update the value for 'i' left parameter (2).
while in second call, you used pre increment operator which changes itself for right parameter and sends updated values for left parameter.
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 ..