This question already has answers here:
Is the output of printf ("%d %d", c++, c); also undefined?
(6 answers)
Determining subroutine argument evaluation order [duplicate]
(2 answers)
Closed 8 years ago.
Today I revisited Pre Increment and Post Increment.
Basic definitions I know.
Pre Increment - Increments the Value and returns the value.
Post Increment - Increments the Value and returns the value prior to increment.
But Doing some combinations of them i am stumped.
Using a basic C program, here is what I tested.
With i =0 initially.
1st Test
printf("%d %d",++i,++i);
Ouput:
2 2
I Expected:
1 2
2nd Test
printf("%d %d",i++,i++);
Ouput:
1 0
I Expected:
0 1
3rd Test
printf("%d %d",i++,++i);
Ouput:
1 2
I Expected:
0 2
4th Test
printf("%d %d",++i,i++);
Ouput:
2 0
I Expected:
1 1
I figured the evaluation might be from the right side or the left side. Maybe from left in case of Pre Increment & Right in case of Post increment. Maybe Pre Increment had a higher priority than Post increment. Some ideas to match the results, but assumption made on one test doesn't explain the other output.
Everything you have is undefined behavior because you are modifying the same variable multiple times between the same pair of sequence points. For example
i = i++;
is undefined as well. There's a deeper discussion here, and there's a nice slideshare that covers this and more "deep C" quirks.
The other issue is the order of evaluation. The arguments are evaluated in an unspecified order, so if you have
f(a(), b(), c());
it can call a, b, and c in any order.
You're mixing undefined behavior and unspecified behavior, so although you could venture a guess to explain why you get the output you do, it's hard to give a satisfactory explanation since it's so random.
Related
This question already has answers here:
Is short-circuiting logical operators mandated? And evaluation order?
(7 answers)
Closed 4 years ago.
#include<stdio.h>
void main()
{
int x=3,y=2,z=0,m;
m=++x || ++y && ++z;
printf("\n %d %d %d %d\n",x,y,z,m); // 4 2 0 1
}
The output of the following code is mentioned as comment in program and I am trying to evaluate how this answer came but I am not able to understand.
I just wanted to know how the program calculates the relative value.
thanks to pmg, i have corrected my original answer (i had an error)
Because the left side of the OR operator (||) is not zero, it doesn't evaluate anything else on that line. This is called a "short circuit operator". In this example you gave, the programmer basically is tricking the compiler. If the argument on the right of the operator does not affect the outcome, it will not execute that code. However in this case, there are increments going on there and they won't be evaluated either.
This will assign "1" to m.
your output should be 3, 2, 0, 1.
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.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
I've got a question regarding an assignment i've been given.
-There are two integeras, a and b, which has the value of -1 and 1 respectively.
What's the value of a and b after running the following code and WHY.
if(!++a)
b+=a++;
When i run this code i'm getting the values 1 and 1. I can not really figure out WHY though... Im getting quite confused over the if statement, could anyone explain this for a beginner trying to learn C?
Your code is equivalent to this one:
int a = -1;
int b = 1;
a += 1;
if (a == 0) {
b += a;
a += 1;
}
You should see why both variables end up as 1 here. Now try to figure out why these codes are equivalent.
if (!++a)
First of all, if (a) is identical to if (a != 0). Weird, but true. Zero means false, any other number means true.
Saying if (!a) inverts that meaning.
++a increments a and returns the new value. (Unlike a++, which increments a but returns the old value, not the new one.)
Putting all that together, this says "increment a and test whether the answer is zero". If a = -1, then this is indeed true.
Usually people would write if (...) {do stuff}, but if the "do stuff" part is only one statement, you can leave out the brackets. We've already established that the condition is true in this case, so the "b+=a++" line is run.
If we put some spaces on that, we have
b += (a++);
So, increment a again, but before that, add it's (old) value to b.
++a gives you the value after increment, so you get 0 which means false. The ! operator makes it true.
Then you basically have
b = b + a;
a++;
So b remains 1 and a gets another increment and ends up at 1 too.
The main concept here is the difference between a++ and ++a. If you use a++ you will first get the value of a and then the value is incremented whereas for ++a the value is first incremented and then returned.
Initially a= -1 when your code will enter if (!++a) and it is pre increment first increment of a will happen so a will become 0 and !0 is 1 and it will enter in the if block.
now b+=a++;
here a++ is post increment so you can devide this statement in two parts.
first b+=a; b+=0 so b will remain 1.
second a++; a will become 1.
This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 9 years ago.
I felt that such an expression should be invalid but I was able to compile it and got the answer 5.
At the end I felt that even if it does answer should be 4 not 5.
int main(void)
{
int i=1;
// how is the next line evaluated ie in what sequence??
i=2+2*i++;
printf("%d",i);
return 0;
}
The output I got was 5. I can not understand how it should give the value.
This is undefined behaviour, since i is modified more than once between sequence points. For instance, this compiler gives 4 as the answer, because it puts the increment after the assignment. Another reasonable answer is 6, if the increment is before the assignment. But, as you've found, the compiler is permitted to make the answer whatever it wants, including 5.
See here for more about sequence points and undefined behaviour.
This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 9 years ago.
I have encountered a sorcery today.
http://codepad.org/VW2vTpWw
Language: C
Code:
#include <stdio.h>
main()
{
int i = 5;
i = i++;
printf ("%i", i);
}
Output:
6
How? Why?
This is supposed to be tricky code but the other way around. The negligent programmer would think that i = i++ is just simple increment but it's not. Yet it works like one here. It's supposed to be 5! Like in JavaScript.
What should be happening.
i gets the value of 5.
i++ returns 5.
i is post incremented by i++ (to 6).
i gets the value of 5 (returned by i++).
the value of i (5) gets printed.
Yet it is 6.
I haven't been able to find a description to this on SO, or the whole internet (just the other way around).
What is broken here?
Please explain.
It is undefined behavior to store more than once to an object without an intervening sequence point.
In particular, your step 3 and 4 have no defined ordering, the increment (and store) or the store could happen first.