Really, now there aren't 101 mistakes, but there will be all (with your help). This post want to be a vademecum for beginners to avoid most common mistakes. It could be also awesome, for example IMHO the most important mistake is the one you made when you decided to enter the profession of programmer.

Please send the most common errors you made when you were a beginner, and if you want, vote your favorite. We will update this list to build a real top ten.


So let's start our list:

1. Starting the profession

You can do a lot of things in your life, why did you decide to become a programmer?



2. Non-terminated comments

 "accidentally" terminated by some subsequent comment, with the code in between swallowed.

        a=b; /* this is a bug

        c=d; /* c=d will never happen */

This code compiles fine but doesn't do the work

 


3. Accidental assignmentNon-terminated comments

if(a=b) c;  /* a always equals b, but c will be executed if b!=0 */

It's too simple confusing the compare operator with the assignment.


 

4. Boolean expression evaluation

if(0<a<2)       /* this "boolean" is always true! */

    c;

Always true because (0<a) generates either 0 or 1 depending on if (0<a), then compares the result to 2, which is always true, of course.

 

if(0<a<-1)      /* this "boolean" is always false! */

    c;

Always false because (0<a) generates either 0 or 1 depending on if (0<a), then compares the result to -1, which is always true, of course.

C doesn't really have boolean expressions, it only pretends to.

 

5. Function returning values

Often newbies forget to return values…. and sometimes experts do this, too.

for example

    int foo (a)

    { if (a) return(1); } /* buggy, because sometimes no value is returned  */

New C compilers returns a warning in this case, but older compilers do not.

 

6. Macro expansion

All the mistakes in the Macro expansion are very sneaky, because they don't return any warning also with newer compiler.

 

6.a Missing braces

Imagine you want to print two different lines when a condition is true, you can define a macro like the following:

#define f(x) printf("%d",x); printf("next value is %d\n", x+1)

It's clearly wrong, if in your source code you insert the following

if(condition)

    f(x);

it expands in

if(condition)

    printf("%d",x);

printf("next value is %d\n", x+1);

WRRRROOONNNNGGGG.

The correct way to write the macro is as follows:

 

#define f(x) {printf("%d",x); printf("next value is %d\n", x+1)}


Part two in the next fifteen days.

Gg1