Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
711 views
in Technique[技术] by (71.8m points)

c++ - What is &&& operation in C

#include <stdio.h>

volatile int i;

int main()
{
    int c;

    for (i = 0; i < 3; i++) 
    {
         c = i &&& i;
         printf("%d
", c);
    }

    return 0;
}

The output of the above program compiled using gcc is

0
1
1

With the -Wall or -Waddress option, gcc issues a warning:

warning: the address of ‘i’ will always evaluate as ‘true’ [-Waddress]

How is c being evaluated in the above program?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It's c = i && (&i);, with the second part being redundant, since &i will never evaluate to false.

For a user-defined type, where you can actually overload unary operator &, it might be different, but it's still a very bad idea.

If you turn on warnings, you'll get something like:

warning: the address of ‘i’ will always evaluate as ‘true’


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...