Is that a valid expression? If so, can you rewrite it so that it makes more sense? For example, is it the same as (4 > y && y > 1)
? How do you evaluate chained logical operators?
&n writes the address of n . The address of a variable points to the value of that variable.
C operators are one of the features in C which has symbols that can be used to perform mathematical, relational, bitwise, conditional, or logical manipulations. The C programming language has a lot of built-in operators to perform various tasks as per the need of the program.
The statement (4 > y > 1)
is parsed as this:
((4 > y) > 1)
The comparison operators <
and >
evaluate left-to-right.
The 4 > y
returns either 0
or 1
depending on if it's true or not.
Then the result is compared to 1.
In this case, since 0
or 1
is never more than 1
, the whole statement will always return false.
There is one exception though:
If y
is a class and the >
operator has been overloaded to do something unusual. Then anything goes.
For example, this will fail to compile:
class mytype{
};
mytype operator>(int x,const mytype &y){
return mytype();
}
int main(){
mytype y;
cout << (4 > y > 1) << endl;
return 0;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With