int main()
{
int i, c;
i:
for (i = 0; i < 3; i++) {
c = i &&&& i;
printf("%d\n", c);
}
return 0;
}
The output of the above program compiled using gcc
is
0
1
1
How is c evaluated in the above program?
The use of labels as values is a gcc
extension (see here). Your expression segment:
c = i &&&& i;
equates to:
c = i && (&&i);
where &&i
is the address of the label i
.
Keep in mind you're combining two totally different i
"objects" here. The first is the i
variable which cycles through 0, 1, 2
, while the second is the label i
, for which the address is always some non-zero value.
That means that the result placed in C will be 0
(false) only when the variable i
is 0
. That's why you're getting the 0, 1, 1
sequence.
As an aside, I give serious thoughts to "employee management practices" if one of my minions bought me code like this for production use. Anything that removes the possibility of monstrosities like this would be a good thing in my opinion :-)