user3450148
user3450148

Reputation: 962

GNU c++ compiler option to flag if (a = b)

Having brain spasm this morning. Can't remember the GNU compiler option to flag

if (a = b) 

as a warning/error because it is an assignment and not a condition.

Upvotes: 1

Views: 146

Answers (1)

0___________
0___________

Reputation: 67721

The easiest way is to enable all warnings and see what is reported:

int main(int argc, char **argv){
    if(argc=0) ;
}

compile with -Wall

you will get the warning:

<source>: In function 'int main(int, char**)':

<source>:3:12: warning: suggest parentheses around assignment used as truth value [-Wparentheses]

    3 |     if(argc=0) ;

      |        ~~~~^~

Compiler returned: 0

So the option is: -Wparentheses

https://godbolt.org/z/6ac1zG

Upvotes: 3

Related Questions