Reputation: 11
#include<stdio.h>
main()
{
(5%5)?1:0&puts("fizz");
}
The code above prints fizz
as expected. But I don't understand how the bitwise &
operand works with 0&puts("fizz")
. What is the working behind it? What is the value of the expression?
Someone please explain
Upvotes: 1
Views: 163
Reputation: 9930
(5%5)
is evaluated to 0
which is false. 0&puts("fizz");
0
is bitwise AND'd with the return value of puts
which can only be found by executing the function which prints fizz.0
) is discarded.Upvotes: 4
Reputation: 58271
The expression:
(5 % 5)? 1 : 0 & puts("fizz");
^ ^
0 == False executes
puts prints: "fizz"
. The returned value from puts function is bitwise & with 0
and result (that should be 0
) is discarded.
So your expression is equivalent to (5 % 5)? 1 : puts("fizz");
in effects.
Upvotes: 3
Reputation: 532
The puts()
returns a non-negative number when sucessful. if (5%5)
will return false. Therefore, the return value from puts()
is bitwise &
with 0
.
Upvotes: 0
Reputation: 1282
The puts()
function returns a non-negative number if it completes successfully. This non-negative number is then ANDed with the zero.
Upvotes: 0
Reputation: 399833
Well ... 5 % 5
is 0 (false), so the ?:
part goes on to evaluate the expression to the right of the colon.
That means evaluating 0 bitwise-and:ed with the return value of puts()
, so obviously the function must be called.
Upvotes: 2