2 ms·
if(x = 3) if(3 = x) If you make a habit of making the left side of == immutable, the compiler will let you know when you type = instead. Of course, gcc throw
by dllthomas 14y ago
if(x = 3)
if(3 = x)
If you make a habit of making the left side of == immutable, the compiler will let you know when you type = instead. Of course, gcc throws a warning unless you type
if((x = 3)) if you genuinely want to do the assignment inside the if, which works just as well but requires tool support.
- DHowett 14y agoWhat tool support does it require? I certainly hope there are no tools generating assignments in conditional expressions, and the only other tools that need to support it clearly understand parentheses.
- dllthomas 14y agoThe warning itself is dependent on your compiler squawking when it sees "if(x = 5)". The C spec doesn't require this. On the other hand, "if(5 = x)" will be thrown out by any C compiler.