Sunday, May 24, 2015

On C Programming: Some Pitfalls

I have programmed in C for almost 20 years, and over the years, I have my fair share of lessons learned. Here are some of the pitfalls that are very common. Learn these and you don't have to spend hours or even days to debug

  • = and == confusions
 The '=' operator is for assignment, while '==' is for comparison. Mix these up and you will have a major headache as the variable has been changed (or not)
  •  The 'if' block
Consider the following, assume x, y, and z are defined somewhere else:
if (x == 3)
  y = 43;
  z = 0;

{do something more}

Guess what? if x is equal to 3, only the statement 'y = 43;' will be executed, while 'z = 0' will be executed no matter what, which isn't the initial intention.

There are a list of programming pitfalls here

No comments: