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
Look at the program-
#include<stdio.h>
int main()
{
int x=2, y=6, z=6;
x = y == z;
printf("%d",x);
}
I thought the output would be 6. I think that z's value is 6 and z is equal to y. So y has the value 6. And the y(which value is 6) is assigned to x. So i think the answer should be 6. But upon execution I find that the the answer is 1. Can anybody explain, why 1 is the output of this program?
x = y == z; is parsed by the compiler as x = (y == z); because of the operator precedence of == is higher than that of = operator.
y == z checks whether y is equal to z or not and produce a Boolean value (either 0 or 1) based on the result of comparison. Since y = 6 = z, y == z returns 1 causing the value of x to be 1. Hence the output is 1.
I think,
You are checking y==z. (y==z) will return a boolean value. So that will be true(1 in C, true in C#). thus the value 1.
Assignment operator works from right to left. so y == z would be first executed. Double equals operator in c checks if both values are same or not. If they are it returns true which is nothing but 1 in c.
This value will then be copied to x. C doesn't have boolean data type it uses integers to store boolean. 1(or any non zero) means true and 0 means false.
if you expected to copy value of z to x, the below line would be more appropriate...
x = y = z; better yet x = z; would also work.
Caveat, it's been a while since I've worked in c.
The initial assignments work as you expect. x=2, y=6, and z=6.
The key is that == is a boolean equals operator. So it basically asks does y equal z? True or false? So your line evaluates in this order:
x = y == z; // Does y equal z? Evaluates to TRUE because 6 equals 6
x = TRUE; // TRUE casts to 1 as an int
x = 1;
Related
I have to analyze what some code in C does, and I have a doubt about what happens in a certain line. The code is
#define PRINTX printf("%d\n", x)
void problem() {
int x = 2, y, z;
x *= 3 + 2; PRINTX;
x *= y = z = 4; PRINTX;
x = y == z; PRINTX;
x == (y = z); PRINTX; // This line is the problem
}
This code snippet prints the resulting numbers:
10
40
1
1 // This result **
the problem is that I'm still trying to figure out why does the last line prints out x = 1, when the operation is x == (y = z). I'm having trouble finding out what that 1 means and the precedence of the operations. Hope someone can help me! :)
Nothing in the last statement changes the value of x, so its value remains unchanged.
Parens were used to override precedence, forcing the = to be the operand of the ==.
An operator's operands must necessarily be evaluated before the operator itself, so we know the following:
y is evaluated at some point before the =.
z is evaluated at some point before the =.
x is evaluated at some point before the ==.
= is evaluated at some point before ==.
That's it. All of these are valid orders:
z y = x ==
y z = x ==
x y z = ==
etc.
But whenever x, y and z are evaluated, we can count on the following happening:
= assigns the value of z (currently 4) to y and returns it.
== compares the value of x (currently 1) with the value returned by = (4). Since they're different, == returns 0 (which isn't used by anything).
As you see, nothing changed x, so it still has the value it previously had (1).
In the last statement, nothing is changing the value of x. We are testing if x equals something, but we aren't changing it's value.
So it continues having the same value as it had in the previous statement, in particular, a value of 1.
the reason is because the == operator checks if the 2 numbers are equal, and returns 1 if equal and 0 if not equal that is why it returns one you can check by making x= 1 and y=2 and using the == operator between them
The comparison result of x and assignment of y with (y = z) is discarded. Last line could have dropped the compare: y = z; PRINTX;.
The assignment is not subsequently used either, so the line could have been PRINTX;.
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))
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Using only bit-wise and logical operations, how can I detect if any bit of x equals to 1 in C? No loops, if/else, ==, or !=
Someone proposed (x & ~0) && 1, but I've been looking at it for hours and still don't know how (x & ~0) && 1 can detect if any bit of x equals to 1
!!x will do it.
This will convert all values to 1, except for 0 which stays 0. And compilers can optimize this intelligently.
0 is a number with all its bits set to 0.
~ is bitwise NOT - it changes all 0 bits to 1 bits and 1 bits to 0 bits, so ~0 gives a number with all bits set to 1.
& is bitwise AND - & of two numbers returns a number where each bit corresponds to the logical AND of the corresponding bits in each of the numbers. Since ~0 consists of all 1 bits already, x & ~0 will simply return a number where each bit corresponds to the same bit in x, thus it will return x.
See Wikipedia on bitwise operations (AND and NOT) for more information.
Finally && is a boolean AND operator which will return 1 only if both sides are non-zero. 1 is not zero, thus it simply checks if (x & ~0) = x (as we established above) is not zero, thus if any of the bits in x are 1.
You should notice that there's a lot of redundancy going on here - as already pointed out, you can just use x instead.
Is this your class assignment?
You wrote "how can I detect ...", but what do you expect for the result of your "detection"?
If the result should be an integer to be fed to if/else, then you can directly feed the original x to if/else, because C's if/else can work with any integers and when "any bit of" the input equals 1, the then clause will be executed. Only when the input is exactly 0, the else clause will be executed. But if your question is your class assignment, this will not be the answer you're looking for.
If you want either 0 or 1 and don't want anything else, use #StilesCrisis's answer. Because
int y = ! x
is equivalent to
int y;
if (0 == x)
y = 1;
else
y = 0;
and therefore
int z = !! x
(whose verbose version is
int z = ! ( ! x )
) is equivalent to
int z;
if (0 == x)
z = 0;
else
z = 1;
Finally, (x & ~0) && 1 is as pointless as x + 0.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
C comma operator
I came across a line of code which I couldn't understand. I remember seeing something similar somewhere.
int x,y,z;
x=(y=2,z=2*y,z+4);
I know that the value assigned to x is 8. Can someone explain me why?
This is equivalent to:
y = 2; // y == 2
z = 2 * y; // z == 4
x = z + 4; // x == 8
The operands of the comma operator are evaluated from left to right and the result is the value of the right operand.
the comma operator separates the previous values, and the last item in the comma is returned as the result, e.g.
a = b,c
assings the value of c to a. The parentheses here do essentially nothing, btw
So you have two assignments, then a statement, whose result is returned and assigned to x
#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.