5
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?

manav m-n
  • 11,136
  • 23
  • 74
  • 97
  • 12
    You thought this would be fun?!! – Mark Garcia Dec 19 '12 at 07:06
  • 31
    How long before somebody asks about the `&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&` operator? – Jerry Coffin Dec 19 '12 at 07:07
  • 2
    Actually, Jerry, gcc complains if you add even _one_ more. – paxdiablo Dec 19 '12 at 07:11
  • Doesn't compile in MSVS (it would be interesting if it did). – Luchian Grigore Dec 19 '12 at 07:12
  • 6
    @JerryCoffin, I think this is not entirely stupid question. This program shows different warning messages when you have `&&&` and it doesn't compile if there are 5 or more `&`. Will give +1. Though I don't know why it doesn't compile in MSVC. Seems to be a gcc extension. – iammilind Dec 19 '12 at 07:14
  • 11
    You [previously asked](http://stackoverflow.com/questions/13946938/what-is-operation-in-c) about `&&&`. Next you would increase an `&` and ask a new question. – sgarizvi Dec 19 '12 at 07:14
  • 1
    Not sure _why_ this is considered too localised. It's a perfectly valid gcc extension. Are we going to close _all_ questions that don't conform to strict ISO C? :-) – paxdiablo Feb 26 '13 at 09:41

2 Answers2

28

In this case it's also parsed as && and &&. First one is logical AND but second && is the address of label i not the address of variable i. (it's gcc extension)

This will be parsed as c = (i) && (&&i); // parenthesis is just to make it human readable.

& gives you the address of variable i. Which you have just asked few minutes ago in this question .

For more about the address of label and values see this gcc extension.

EDIT : since && logical AND always follows the short-circuiting of statement.Hence in first case it will be 0 since it found i=0 so it won't go to &&i(the second part of logical expression).

But in all subsequent cases , i is not 0 so it will give TRUE and will go to next expression &&i which is address of label i (And Address of i will always evaluate to TRUE.)

So the result of the full expression will always be TRUE means 1 except the first case where i is 0. Hence you are seeing the result

0
1
1
Community
  • 1
  • 1
Omkant
  • 9,018
  • 8
  • 39
  • 59
25

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 :-)

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953