...
In this noncompliant code example, an assignment expression is the outermost expression in an if
statement.
Code Block | ||||
---|---|---|---|---|
| ||||
if (a = b) { /* ... */ } |
While the intent of the code may be to assign b
to a
and test the value of the result for equality to zero, it is very frequently a case of the programmer mistakenly using the assignment operator =
instead of the equals operator ==
. Consequently, many compilers will warn about this condition making this coding error detectable by adhering to recommendation MSC00-C. Compile cleanly at high warning levels.
...
When the assignment of b
to a
is not intended, this conditional block is now executed when a
is equal to b
.
Code Block | ||||
---|---|---|---|---|
| ||||
if (a == b) { /* ... */ } |
When the assignment is, if fact, intended, this is an alternative compliant solution:
Code Block | ||||
---|---|---|---|---|
| ||||
if ((a = b) != 0) { /* ... */ } |
...