#include <stdio.h>
int main()
{
int a = 60; // 0011 1100
int b = 13; // 0000 1101
int c = 0;
c = a || b;
printf("%d",c);
return 0;
}
The output for my code is 1. Can anyone explain how that worked?
In this statement
c = a || b; // 0011 1101
there is used the logical OR operator ||
that yields 1 if either of operands is unequal to 0.
From the C Standard (6.5.14 Logical OR operator)
3 The || operator shall yield 1 if either of its operands compare unequal to 0; otherwise, it yields 0. The result has type int.
The bitwise inclusive OR operator |
is written like
c = a | b; // 0011 1101
If you will write like
c = a | | b;
with a blank between the symbols '|'
then the compiler will issue an error. You may not use such a way any binary operator because such an operator expects operands on the left and the right sides of the operator.
Of course, if you will write for example like
c = a + + b;
then there is the first operator +
is the binary plus and the second operator +
is the unary plus operator. That is there is no two consecutive binary operators +
.
You should not mix logical operators ||
and &&
with bitwise operators |
and &
.