[TUT][C] Never mistake test for equality with assignment

Go To Last Post
9 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

A very common error in C, especially for the beginner, is to do an assignment where a test for equality was wanted.

What is done is:

if( x = 0xBE ) /* Error in any general case */

when what is wanted was:

if( x == 0xBE )

A very simple way to prevent this is to always place the constant on the left:

if( 0xEF == x )

will do what is wanted, while:

if( 0xEF = x )

will refuse to compile at all, because it is impossible to assign a variable to a constant in C.

Best way to debug a program is to not put the bug in the program in the first place, so have the compiler tell you when you have made this error.

I've heard some silly arguments against this simple bug proofing technique over the years, and no rational ones.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

There is no rational argument against it, but it just looks silly :)

Turbo C gives the 'Possibly incorrect assignment' warning when it sees an assignment inside an if.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

bpaddock that is certainly good advice. You will find the same advice on virtually any text on "bulletproof" coding. It can look a bit odd, but it definitely helps avoid bugs that can be hard to spot.

Thanks for posting it here, I'm sure more than a few people can benefit from this advice.

Writing code is like having sex.... make one little mistake, and you're supporting it for life.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I remember the first time I encountered this when we integrated a TCP/IP/HTTP stack from a third party and all their code put the constant on the left. At the time I thought my head was going to explode but it does make a lot of sense but I still can't bring myself to do it myself.

Beginners just starting out in C would probably be well advised to adopt it into their own coding standard though.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

glitch wrote:
bpaddock that is certainly good advice. You will find the same advice on virtually any text on "bulletproof" coding.

I'm always looking for such things for my Software Safety
site and blog. http://www.softwaresafety.net

Like I said better to not put the bugs in, than try to get them out...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
I remember the first time I encountered this when we integrated a TCP/IP/HTTP stack from a third party and all their code put the constant on the left. At the time I thought my head was going to explode..

A former colleague of mine missed the essence of what was meant to be accomplished by this technique, and started putting all constants of all conditionals on the left. That truly does want to make your head explode,

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

My C++ professor guaranteed that all of us would make the assignment error when we wanted to test for equality. I laughed and said "no way, I wouldn't make such a silly error".

I have made this mistake more than once...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

gcc -Wall will warn you, in general one should listen to compiler warnings :)

(If you want to do it intentionally then if ((x = 1)) will not produce a warning.)

"warning: suggest parentheses around assignment used as truth value"

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
-Wall -Werror