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.
Related
This question already has answers here:
issue with assignment operator inside printf()
(3 answers)
How to understand the behaviour of printf statement in C?
(5 answers)
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 3 years ago.
I've been studying C for about a year now, and I came across this above when I was just playing around. I first thought maybe it's a case of assignment precedence (i.e. x=10 happens first), but then I tried
printf("%d %d %d", x==5, x=10, x<6);
and it outputs
0 10 1. Could someone please explain to me why/what is going, as this seems extremely baffling to me and I'm starting to think it's undefined behavior perhaps?
This is indeed undefined behavior. Arguments to functions are evaluated in an unspecified order, so doing anything that relies on that order becomes UB.
It looks like your compiler goes right-to-left (at least in this instance). That's a reasonable way to do it. But since it's UB, don't count on it always doing that.
This question already has answers here:
What does the comma operator , do?
(8 answers)
Closed 6 years ago.
I tried executing the following c code:
#include <stdio.h>
int main()
{
int i=2000,j=100;
printf("%d",(j,i));
}
I gave different values to i and j and found the output. I always get the output as the value contained in the second variable. Does the expression always give the last variable as a result or does it have any other meaning?
You are using the comma operator.
The output of your code is 2000, because the comma operator evaluates the two expressions and returns the value of the second operand. More details can be found in this SO answer.
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.
This question already has answers here:
Precedence of && over || [duplicate]
(4 answers)
Closed 9 years ago.
I was going through a C objective book where a question comes:
#include<stdio.h>
#include<conio.h>
int main()
{
int i,j,k;
i=j=k=1;
k=++i||++j&&++k;
printf("%d %d %d",i,j,k);
return 0;
}
The output is:
2 1 1
In my view:
k is incremented.
j is incremented.
i is incremented.
k&&j will happen.
i|| (k&&j)
So the output should be i=2,j=2,k=1. What am I missing?
The expression k=++i||++j&&++k; causes undefined behaviour. You are trying to assign to k twice without an intervening sequence point.
Even if the assignment were to a different variable, your steps would be inaccurate - the logical operators have short-circuiting behaviour.
Edit: OP says he changed the expression to a=++i||++j&&++k. I'm going to rewrite it fully parenthesized and with some spaces:
a = ++i || (++j && ++k);
In this case, only the ++i is evaluated, due to short-circuiting behaviour of the || operator.
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.