This question already has answers here:
Logical equality in C
(4 answers)
Closed 3 years ago.
What does
return x == y;
mean?
What does
return x == y;
mean?
Do a logical comparison between x and y which evaluates to an int holding either 1 or 0 depending on x and y being equal or not.
End the function and return the result as per 1..
First of all, let me make you understand what x == y means.
x == y compares x and y. The result of x == y will be true if x and y are equal, false otherwise.
In C, true is equivalent to any non-zero value (default is 1) and false is equivalent to zero.
So, if x is equal to y, x == y is equal to 1. Otherwise it is 0.
Finally, return x == y means that the value returned by the function will be 1 if both x and y are equal, 0 otherwise.
Consider the function,
int isEqual(int x, int y) {
return x == y;
}
int main() {
printf("%d", isEqual(1, 1)); // 1
printf("%d", isEqual(1, 2)); // 0
return 0;
}
== compares values of two variables. The compare results in true or false, so if x and y are equal, the result is true, else the result is false.
The return statement returns this result from the function.
In C, false is represented for a 0, and true for each other number (positives and negatives). So the function returns a 0 or a 1
According to the C Standard (6.5.9 Equality operators)
3 The == (equal to) and != (not equal to) operators are analogous to
the relational operators except for their lower precedence.108) Each
of the operators yields 1 if the specified relation is true and 0 if
it is false. The result has type int. For any pair of operands,
exactly one of the relations is true.
So in this return statement
return x == y;
there is used an expression with the equality operator ==. The value of the expression either 1 or 0 depending on whether x is equal to y or not. So the return statement returns either 1 or 0.
You could rewrite the return statement like
int result = ( x == y );
return result;
Consider for example an if statement in the condition of which there are compared two strings
if ( strcmp( s1, s2 ) == 0 )
{
puts( "The strings are equal each other." );
}
//...
You could write a separate function that would look like
int equal( const char *s1, const char *s2 )
{
return strcmp( s1, s2 ) == 0;
}
In this case the if statement would look like
if ( equal( s1, s2 ) )
{
puts( "The strings are equal each other." );
}
//...
The expression x == y yields an integer value, either 0 (when x is not equal to y) or 1 (when x is equal to y).
This gives you a bool (or boolean). If x is equal to y, it will return true, if not, then false. This will work the same in most programming languages that use the == operator.
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;.
Came across some code that has an OR operator (||) in the return line. Could someone explain what that does ?
Here's something that looks like it:
int main()
{
...
return (1 || 0);
}
But really it was a function (that returned 1 or 0) instead of the 1 and 0:
int main()
{
...
return (foo(x++, y) || foo(x, y++));
}
a || b evaluates to 1 if a is non-zero irrespective of the value of b, evaluates to 1 if a is 0 and b is non-zero, else it's 0.
So 1 || 0 is 1, as would be 2 || 0.
Note that b is not evaluated if a is non-zero: so if b was a function, it would not be called in such a case. So in your example, foo(x, y++) is not called including the evaluation of y++ if foo(x++, y) is non-zero.
Note that the type of a || b is an int irrespective of the type of the arguments. Cf. C++ where the type is a bool.
Logical OR has a short circuit property. The RHS is only evaluated if the LHS is evaluated to false.
In this case, since the operands are function call, so
the LHS function call foo(x++, y) will be made.
If the return value is 1 (TRUTHY), the RHS will not be evaluated and the value 1 will be returned.
If the returned value is 0, the foo(x, y++) function call with made, and the return value will be based on teh return value of the function call.
#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.
Since the associativity of '?' is from right to left,any 2 consecutive '?' operators must be treated as such,Right?
Now,
int x=-1;
int y=x?x++?x:-1:1;
I expect this to be executed as:
int y = x ? (x++?x:-1) : 1;
Now since its being executed from right to left,when encountering the first '?' in the statement,x's value is 0 and the expression is as
int y= x? 0 : 1;
hence i expected y to be 1,but it shows Zero on my dev-cpp.Where am i wrong?
You have the order of evaluation wrong. In a ? b : c, a is always evaluated first, then either b or c is evaluated.
I've marked up your example so that I can identify subexpressions:
c
int y=x?x++?x:-1:1;
a bbbbbbbb
(a) is evaluated, yielding -1, so (b) is evaluated. There, x++ is evaluated, yielding -1 again, so (c) is evaluated. At this point, x is 0.
Or, with more verbose, clearer code, it's as if you said:
int x = -1;
int y;
if (x != 0)
{
int new_x = x + 1;
if (x != 0)
{
y = new_x;
}
else
{
y = -1;
}
}
else
{
y = 1;
}
Operations:
Assign y to value =
if(x): --> x = -1, so true as it is non-zero
{
if(x): --> x = -1 ,so true as x will increment later due to post increment
x= x +1; --> increment x, so x = 0 . This is the value assigned. So y = 0;
else:
-1
}
else:
{
1
}
Hope this helps!
The answer to your question is that in C/C++ int y = x ? (x++?x:-1) : 1; we will hit two sequence points at ?. Any update operations to variable with in a sequence point will be effective after that sequence is over. So lets look at our example in hand.
First sequence point is first ? from left.
x=-1; (Actual Value)
x=-1; (Value used in expression)
y=-1?(x++?x:-1):1;
Second sequence point is second ? from left. As mentioned above the update operations are effective after sequence so even though x++ is there the value used in this sequence is -1 and updated value will be used in following.
x=0; (Actual Value, bcoz of x++)
x=-1; (Value used in expression)
y=-1?x:-1;
Now it will be
x=0; (Actual Value)
x=0; (Value used in expression)
y=x;
y=0;
Hope this make sense now.
I am trying to figure out what == sign means in this program?
int main()
{
int x = 2, y = 6, z = 6;
x = y == z;
printf("%d", x);
}
The == operator tests for equality. For example:
if ( a == b )
dosomething();
And, in your example:
x = y == z;
x is true (1) if y is equal to z. If y is not equal to z, x is false (0).
A common mistake made by novice C programmers (and a typo made by some very experienced ones as well) is:
if ( a = b )
dosomething();
In this case, b is assigned to a then evaluated as a boolean expression. Sometimes a programmer will do this deliberately but it's bad form. Another programmer reading the code won't know if it was done intentionally (rarely) or inadvertently (much more likely). A better construct would be:
if ( (a = b) == 0 ) // or !=
dosomething();
Here, b is assigned to a, then the result is compared with 0. The intent is clear. (Interestingly, I've worked with C# programmers who have never written pure C and couldn't tell you what this does.)
It is "equals" operator.
In the above example, x is assigned the result of equality test (y == z) expression. So, if y is equal to z, x will be set to 1 (true), otherwise 0 (false). Because C (pre-C99) does not have a boolean type, the expression evaluates to an integer.
Equality. It returns 1 if the operands are equal, 0 otherwise.
== means "is euual to". This operator has higher precedece than = (equal to) operator. So the equation x = y == z; will try to assign result of y==z to variable x. which is 1 in this case.
int main()
{
int x = 2, y = 6, z = 6;
x = y == z;
printf("%d", x);
}
let`s start like this:
x = (6==6)
It asks is 6 equivalent to 6?: true
x = true, but since x is an int, x= 1
The new value of x is 1.
The following is printed:
1
It's saying
X will equal either true/1 or false/0.
another way to look at that line is this:
x = ( is y equal to true? then true/1 or false/0 )
== operator used for equality..
here in u r example
if y is equal to z then x will hav true value otherwise x will hav false
Think about it like this:
= means give something a value.
== means check if it is equal to a value.
For example
int val = 5; //val is 5
//= actually changes val to 3
val = 3;
//== Tests if val is 3 or not.
//note: it DOES NOT CHANGE the value of val.
val == 3;
int new_val = val == 3; //new_val will be 1, because the test is true
//the above statement is the same as
bool is_val_3 = false;
if( val == 3 )
is_val_3 = true;
int new_val;
new_val = is_val_3;
//putting it together,
val = new_val == 2; //sets val to 0. do you understand why now?