Here is my problem: x is 11, y is 6 and z is 1.
what is the value of w?
w = x == y || x != y && z > x;
I dont understand how to decide whether I should evaluate the || or the && first.
&& has higher precedence than ||. What else does one need to know?
x == y || x != y && z > x;
x != y evaluates to true, and z>x evaluates to false so x != y && z > x evaluates to false. So we have:
x == y || false;
Now since x==y is false we get:
false || false;
However order of evaluation does not depend on precedence if that is where you were confused. For a deeper understanding about what i said please read the answer here to my question by Jerry Coffin.
Operator Precedence vs Order of Evaluation
That would be evaluated like this w = (x == y) || (x != y && z > x) wikipedia has the full order for all operations http://en.wikipedia.org/wiki/Order_of_operations#Programming_languages
Take a look at http://www.isthe.com/chongo/tech/comp/c/c-precedence.html for the order of precedence. In this case && has higher precedence than ||.
In case you are not sure, just use bracket. Putting a bracket is much more useful rather than having such a headache.
Related
According to my limited knowledge logical operators have left-right order of evaluation. By that rule it should evaluate x && y which is 1 and then it should evaluate == 1 in the if statement. But that is what not happening. Could anyone help me with this.
int main()
{
int x = 1, y = 2;
if (x && y == 1)
printf("true\n");
else
printf("false\n");
}
The order of operations is different than what you think.
Your expression is equivalent to
x && (y==1)
which is false in your case.
try this.
because of https://en.cppreference.com/w/c/language/operator_precedence
int main()
{
int x = 1, y = 2;
if ((x && y) == 1)
printf("true\n");
else
printf("false\n");
}
Try not to think about order of evaluation too much. It will often confuse you. The more important things to focus on is how the operators are grouped together and what the expression means.
The && operator has relatively low precedence. Almost any time you use it, it will have the form
if( condition_1 && condition_b )
and the interpretation is the obvious: "if condition_a is true and condition_b is true".
In your case, condition_a is just x, and condition_b is y == 1. So the interpretation is "if x is true and y equals 1".
What does it mean for x to be true? Simply that it's not zero. So we could expand this further: it's as if you had written
if ( x != 0 && y == 1 )
and the interpretation is "if x is not equal to 0 and y is equal to 1".
Remember, precedence says how the operators are grouped together. In your case, it's as if you had written
if ( x && ( y == 1 ) )
The == 1 part goes with the y. == binds more tightly than &&.
In terms of "order of evaluation", you can say that, yes, the compiler emits code that figures out whether x is true, and code that figures out whether y is equal to 1, before it uses && to determine whether they're both true. In the case of &&, we also know that it will decide whether x is true before it decides whether y is equal to 1. (But this is a rather special property of the && and || operators. It does not apply to most of the rest of the more ordinary operators like + and /.)
Because y == 1 is false as y = 2, written before if statement.
It does not evaluate they way you think. It evaluates x and then y == 1 and then &&, which is in your case in 1 && 0 and you have false result.
Here
if (x && y == 1)
above statement is equivalent to
if(x && (y==1) ) /* y==1 performed first, which is false */
As you can see here https://en.cppreference.com/w/cpp/language/operator_precedence
You want to get true as output, then first perform x&&y by keeping ().
if ( (x && y) == 1) { /* now () has higher precedence, so it perform x&&y first which results in true(1) and then 1==1 results in true */
}
else {
}
This is because of operator precedence.
Because of operator precedence the expression if (x && y == 1) results in if (x && (y == 1)).
So, the whole expression results to false because y==1 is false.
Use parenthesis and change it to if ((x && y) == 1) to get desired result.
First this is the question I'm working on:
Evaluate each of the following expressions in C:
int x=1, y=7, z=0;
char a='m';
1) a ? y-x : x–y
2) x = 5 ? (y = z) : (z = y)
I understand the rest of the questions but number (1) confuses me... isn't it supposed to be a logical expression?
I mean 'm' cannot be true nor false; how can I answer this question? Is it simply "Error"? Or is there something I missed?
For number (2) the statement (z=y) should execute which changes z to 7, but isn't it supposed to be x == 5 and I tried it on a terminal and it changes both x and y to 0.
What am I missing?
In C ANY numeric type can be evaluated as a bool, and for such types, any non-zero value is 'true'. So since the character 'm' is non-zero (only '\0' is zero), it is "true"
Precedence -- All operators in C have precedence, and ?:, while lower than most, is higher than assignment operators. So this expression is equivalent to:
x = (5 ? (y = z) : (z = y))
I know what is short circuit evaluation in C.
a && b (operand b is not checked if a = 0)
a || b (operand b is not checked if a = non zero)
But I am stuck at this question
int x = 0;
if (5 || 2 && ++x)
printf("%d", x);
This outputs 0.
My first thinking goes as follows:
According to precedence table , precedence is ++, &&, || (descending order)
++x: evaluated.x becomes 1.
2 && ++x evaluated. Both operands are evaluated.
|| is evaluated.
But according to this, 1 should be printed, not 0.
My second thinking goes as this:
5 || anything
anything is not evaluated because of short circuit evaluation, so no precedence comes into play here.
The expression 5 || 2 && ++x is equivalent to 5 || (2 && ++x) due to operator precedence.
The run time evaluates the expression 5 || 2 && ++x from left to right.
As we know in OR if first condition is true it will not check the second condition.
So here 5 evaluated as true and so (2 && ++x) will not be performed.
That's why x will remain 0 here.
Correct. The expression is short circuited. You can test it with this.
if(5 || ++x) {
printf("%d\n",x);
}
Please explain output of this program.
#include<stdio.h>
int main()
{
int x=0;
x ^= x || x++ || ++x || x++;
printf("\n%d",x);
}
O/p is 3 (http://codepad.org/X49j0etz)
According to me output should be 2.
as || is a sequence point as far as i remember.
so expression becomes.
x ^= 0 || 0 || 2 || 2;
so after evaluation of this expression(x || x++ || ++x || x++;) x becomes 3
x = 3 ^ 1
so x becomes 2;
I'm pretty sure the answers claiming undefined behaviour are correct, but there is also a simple explanation for how you can arrive at the result of 3.
Just consider that the last x++ is never evaluated because the last || operation short-circuits, and let's assume that the side effects are applied before the ^= is evaluated. Then you are left with
x = 2 ^ 1;
Unsurprisingly resulting in 3.
This is an undefined behavior in c. Because we cannot predict in which direction the expression is evaluated
#include<stdio.h>
main()
{
int x = 5, y = 10, z = 10;
x = y == z; // This computational expression causes the value of x to be 1. I fail to understand why
printf("%d\n", x); //Why is the value of x 1 here.
}
I fail to understand the statement x = y ==z;
According to me - x = 10 since y == z. z=10 and is stated to be equivalent to y. The value of y is then assigned to x - x = y
You assign the result of the comparison »Is y equal to z« to x, which is 1, i.e. true.
Note the different operators:
x = ... // assignment
y == z // comparison with either 0 (false) or 1 (true) result
Let's break the program down a little bit further:
You initialize x to 5 and y and z to 10.
You perform a comparison (see above) of y and z without caring for the result. So that's a line that can safely be ignored. But it results in 1 since y is equal to z.
You print the current values of all three variables.
You perform the same comparison, this time assigning the result to x. x now has the value »Is y equal to z«, which is 1 in this case.
Because of operators precedence
x = y == z;
is the same as
x = (y == z);
Now as y == z evaluates to 1, so x value is 1 after the statement.
y == z returns 1 if they are the same and 0 if they are not and the result of this is set to x
== is a comparison operator, so will return 1 (true) if both of the operands are equal and 0 (false) if they are not.
The statement x = y == z is equivalent to x = (y == z), because == has a higher precedence than =. Because y is equal to z, this will assign 1 to x.
Please refer to Operator Precedence Table. == (Comparison Operator) has a higher precedence over = (Assignment Operator). So, the y == z gets executed first and then yields result of 1 as y and z are having the same values which results in x being assigned a value of 1.
= is the assignment operator and == is the comparison operator. you compare y and z, they are equal, so the comparison returns true which is 1. this value is assigned to x.
the == operator acts first.
Hence it becomes x= (value of y==z);
now since y and z are the same, the value of y==z is 1 that gets assigned to x.
== has a greater precedence than =.
The expression y == z is evaluated to 1.
The instruction x = y == z puts 1 in x.
The instruction printf("%d\n", x); prints the value of x (1).
The precedence of == operator is higher than = operator.
y==z evaluates to 1; since both are equal.
Thisn value gets assigned to x.