Here I will list some of the C programming tips that I have found useful over the years. C provides many ways to shoot oneself in the foot. I have found that I tend to make less errors if I follow these guidelines.
I am assuming that you will eventually be using C to program close to hardware and the original goal of C performance is still very important to you. I will not present any C programming tips that add significant clock cycles to the resulting program.
The C language by default assumes that the programmer knows what s/he is doing and does not place many restrictions. There are many constructs in C that are legal but are likely to be wrong. Compilers can flag many of these constructs and throw off warnings. Running the compiler with warnings set at their maximum level and ensuring that the program compiles with zero warnings saves much time debugging the program at run time.
For gcc the options to set the warnings on are -Wall and -W
gcc -Wall -W
One of the most insidious errors that I used to make was writing
if (x = 6)
when what I meant was
if (x == 6)
I found a good way to avoid making this mistake was to write the constant term first
if (6 == x)
Now if I write
if ( 6 =x )
the compiler will complain and refuse to compile the program.
This is not possible to do when you are comparing two variables for example
if (x == y)
can be mis written as
if(x = y)
Modern compilers will give a warning if you write the statement above. If you really do want to write something like the statement above you should write it like
if( {x = y} )
to disable the compiler warning.
Consider the following code snippet
if (3 == x) if( 4 ==y) printf( "x =3, y=4 ");else { printf ( "Not x =3 ");}
The intent here was to have 2 cases when x is 3 and when x is not 3. The compiler sees it as
if (3 == x) if( 4 ==y) printf( "x =3, y=4 "); else { printf ( "Not x =3 "); }
A simple way around these and similar problems is to put braces around ifs and else. I find the program much easier to read with the braces in there even though the program becomes slightly longer
if (3 == x){ if( 4 ==y){ printf( "x =3, y=4 "); }}else { printf ( "Not x =3 "); }
Putting the braces also avoids inadvertent errors from missing semicolong like the following
if(3 ==x) returnvalue[0] = 1;value[1] = 2;
Here there is a missing semicolon is happily accepted by the compiler. Or the error resulting from an extra semi colon
if(3 == x) ; y= 2;Consider the following code snippet
if (3 == x) if( 4 ==y) printf( "x =3, y=4 ");else { printf ( "Not x =3 ");}
The intent here was to have 2 cases when x is 3 and when x is not 3. The compiler sees it as
if (3 == x) if( 4 ==y) printf( "x =3, y=4 "); else { printf ( "Not x =3 "); }
A simple way around these and similar problems is to put braces around ifs and else. I find the program much easier to read with the braces in there even though the program becomes slightly longer
if (3 == x){ if( 4 ==y){ printf( "x =3, y=4 "); }}else { printf ( "Not x =3 "); }
Putting the braces also avoids inadvertent errors from missing semicolong like the following
if(3 ==x) returnvalue[0] = 1;value[1] = 2;
Here there is a missing semicolon is happily accepted by the compiler. Or the error resulting from an extra semi colon
if(3 == x) ; y= 2;
Kernighan and Ritchie, the inventors of C, mention in the C Programming Manual that "some of the operators have the wrong precedence". I always put parenthesis around operators.
For example instead of writing
value & mask != 0
write
(value & mask) != 0
Instead of writing
msb << 4 + lsb
write
(msb << 4) + lsb
Leave the variable names in function prototype declarationsLeave the variable names in function prototype definitions. They provide additional information to the person reading the function declaration, especially if the variables are properly named.
This is more appropriate to beginners.
&& is the logical AND, while & is the bitwise &.
|| is the logical OR, while | is the bitwise |
Hope you found these C Programming Tips useful. Check back in the Articles section for another set of C Programming tips for embedded programming.