Saturday 12 November 2011

Incorrect Mixing of Equality Operators in C programming

Incorrect Mixing of Equality Operators

Compile-time checking can be helpful in working with the incorrect mixing of equality operators. Consider
the following code fragment:

void foo( int a , int b )
{
if ( a = b )
{
/* some code here */
}
}

This kind of error can be very difficult to spot! Instead of comparing the variables, this function sets a to the
value of b and executes the conditional value if b is nonzero! This action is probably not what the programmer
intended (although it might be). Not only will the code be executed at the wrong times, but the value of a
will be wrong when it is used later.

No comments:

Post a Comment