Reputation: 123
Let me be judged as a Noob in programming, I have been learning obfuscated way of programming in c/c++, as the c compiler compiles a statement from the right hand side towards the left hand side.
I have the following code:
int main(){
int x=5, y=20, z=1;
int k = x > y < z;
printf("%d", k);
return 0;
}
The output returned is 1
, Does this means that
x > y < z
= (x>y) < z
or
x > y < z
= x > (y<z)
I would love if someone would give me link to work on these skills.
Thanks and Regards.
Upvotes: 1
Views: 183
Reputation: 311088
According to the C grammar (6.5.8 Relational operators)
1 relational-expression:
shift-expression
relational-expression < shift-expression
relational-expression > shift-expression
relational-expression <= shift-expression
relational-expression >= shift-expression
1 The relational operators group left-to-right (C++ Standard :) )
And
6 Each of the operators < (less than), > (greater than), <= (less than or equal to), and >= (greater than or equal to) shall yield 1 if the specified relation is true and 0 if it is false.107) The result has type int.
Thus the initializer in this declaration
int k =x>y<z;
is equivalent to
int k = ( x > y ) < z;
As x is less than y then expression x > y
yields 0 and as z is greater than 0 then the full expression yields 1.
The following operators have right-to-left-grouping:
unary operators
conditional operator
assignmnet and compound assignment operators
Upvotes: 2
Reputation: 502
Maybe you should know the Operator precedence and associativity
. You can google or see the book <<c++ primer>>
. First the operator >
and <
have a same precedence. Then we should use the associativity
, there the operator only have left associativity
. So the answer is x> y< z= (x>y)<z)
. I hope this can help you. Remember: 1. precedence; 2. associativity.
Upvotes: 0
Reputation: 214860
First you have to check operator precedence, which is easiest by looking at an operator precedence table. If such a table is decent, it will list the operators > and < in a group called relational operators. All operators in this group have the same operator precedence.
Since the operators > and < have the same precedence, the order in which the operands will get processed is determined by the associativity of that group of operators. For the relational operators, this is left-to-right. Therefore the expression is guaranteed to be processed as (x > y) < z
Upvotes: 1
Reputation: 7199
k = ( 5 > 20 ) < 1= False (0)
k = (0 < 1) = True (1)
printf (%d, k) = 1
Upvotes: 0
Reputation: 76433
Change z=-1
or x=0
and find out. Also change int main()
to the more correct int main ( void )
Changing z = -1
will ouput 0
, whereas k
will be 1
if you assign it x > y == z
if z = 0
. So in short:
k = x > y < z;
is the same as writing
k = (x > y) < z;
left-to-right.
Upvotes: 4
Reputation: 5067
In C the boolean values are numerical values, so:
false -> 0 true -> != 0, hence any number that is not 0 will represent the boolean true.
So in case of your code:
int k = x > y < z;
can be split in the following way:
int k = (x > y) < z;
that will be:
int k = ( 5 > 20) < 1;
the first part evaluates to false (0 that is) then the equality will loook this way:
int k = 0 < 1;
which is true, hence
int k = 1;
Upvotes: 0