What is &&&& operation in C

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 with gcc is

 0 1 1 

How is c rated in the above program?

+4
source share
2 answers

Using labels as values ​​is an extension of gcc (see here ). Your expression segment:

 c = i &&&& i; 

matches: c = i && (& & i); where &&i is the address of label i .

Remember that you combine two completely different objects i here. The first is the variable i , which passes through 0, 1, 2 , and the second is the label i , for which the address always has some non-zero value.

This means that the result set to C will be 0 (false) only when the variable i is 0 . That is why you get the sequence 0, 1, 1 .

As an aside, I give serious thoughts to the “employee management practice” if one of my minions bought me a code similar to this for use in production. In my opinion, everything that removes the possibility of such monsters would be good :-)

+24
source

In this case, it is also analyzed as && and && . The first is logical AND , and the second && is the address of label i , not the address of variable i . (this is a gcc extension)

This will be analyzed as c = (i) && (&&i) ; // The brackets are intended only to make it understandable to humans.

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

For more information on label addresses and values, see this gcc extension.

EDIT : because && logical AND always follows the operator short circuit. In the first case, it will be 0 , since he found i=0 , so he will not go to &&i (the second part of the logical expression).

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

Thus, the result of the full expression will always be TRUE means 1 , except for the first case when i is 0 . Therefore, you see the result

 0 1 1 
+27
source

All Articles