10 C Language Tips For Engineers
10 C Language Tips For Engineers
On its own, the software development process has numerous hazards and obstacles that require navigation in
order to successfully launch a product. The last thing that any engineer wants is challenges resulting from the
language or tool that is being used. It often makes sense or is necessary for the hardware designer to write
code to test that the hardware is working or in resource constrained cases, develop both hardware and
embedded software. The language of choice is still C and despite the advances in tools and structured
programming, time and again basic mistakes occur that lead to bugs and maintenance nightmares. In an
attempt to avoid these C programming pitfalls, here are 10 C language tips for hardware engineers.
Programming languages eventually began to incorporate the idea of a function, which allows the program to
break off to a section of code but rather than requiring another goto statement, when completed the function
returns to the next instruction after the function call. An example can be seen in Listing 2. The result was
improved program structure and readability and ever since this has been considered the appropriate way to
write a program. The very sight or thought of goto statements cause software engineers to cringe and shudder
in distaste. One of the main reasons is that a program with goto’s sprinkled throughout is very difficult to
follow, understand and maintain.
Listing 2: Using a function to control flow
The loop conditionals in the listings are relatively straight forward. The for loop is nothing more than the for
conditional with no conditions. The while loop on the other hand will execute as long as the statement is true
which is the same as having any non-zero value for the condtion.
Tip #3 – Use the appropriate conditional statement for the job
Program execution time can be highly dependent on the type of conditional structure that is selected for
making a decision in addition to the readability of the code. Many hardware engineers are familiar with the
use of the simple if statement; however, sometimes the engineer doesn’t realize that if the first condition isn’t
correct, an else or else if statement can be used. This can save the processor time by not having to evaluate
another conditional statement. An example of this can be seen in Listing 5. In the before code, if Var is equal
to one it will still check to see if the Var is equal to zero; however, in the after code that uses the else, only
the first statement is evaluated and then the code moves on, thereby saving clock cycles and making the code
clearer.
The if/else if/else statements still may not always be appropriate. If there are a number of possible conditions
that need to be checked, a switch statement may be more appropriate. This allows the processor to evaluate
the statement and then select from a list of answers what it should do next rather than continually evaluating a
bunch of conditions. An example can be seen in Listing 6 that corresponds to the same type of example
shown in Listing 5.
Listing 6 – Using switch statements
The moral of the story is simply to keep alternative conditional statement options open and select the most
appropriate for the job at hand. This will ease in understanding the flow of the program by making the
structure straight forward and could squeeze extra clock cycles out of the processor.
Tip #4 – Avoid using assembly language
The natural language for a microprocessor is assembly language instructions. Writing a program in the low
level machine language can result in more efficient code for the processor; however, humans don’t naturally
speak this language and as experience has shown writing assembly language results in misunderstanding.
Misunderstanding then leads to improper maintenance or worse and the result is a system overridden with
bugs. Because of this, the general tip is to avoid the use of assembly language. The detailed truth of the
matter is that most compilers now a day compile very efficient code. Developing in the higher languages like
C/C++ results in a more organized structure which is easier to understand and maintain, the result of which is
overall better code. A simple example can be seen in Listing 7 which compares the assembly to C code to
increment a 32 bit variable.
On the other hand there is lasagna! The noodles are layered, giving the meal structure! Code developed using
layers is not only easier to understand, it has the potential to have one layer removed and a new layer added,
basically allowing for reuse and ease in maintainability. Figure 1 shows an example of a simple software
model that uses the lasagna model
a. Watch out for missing #include files. This can result in the developer looking at a perfectly good line
of code but because the necessary header files aren’t included the compiler flags it as error, indicating
that something is not defined.
b. Watch for missing semicolons. One of the most common errors when writing C code is to forget the
semicolon (;) at the end of a statement.
c. Watch for missing brackets ( } ). Brackets are another common mistake that are either left out on
accident or from mistyping turns into a different character.
d. Watch for missing commas (,). In complex definitions it’s easy to forget the comma!
In general when a strange compiler error pops up, look around at what might have been compiled
immediately before that line. Odds are that is where the mistake is! It may be one line up, half a page or in a
completely different file. Don’t give up though! With some experience, finding the difficult ones eventually
becomes second nature.
Tip #10 Good programmers don’t necessarily write fewer lines of code
It is a common misconception that a good programmer can write fewer lines of code to do something than an
average programmer. Don’t get sucked into this idea! A good programmer has a well thought out and
structured code base. Variables are nicely names and encapsulated, few global variables exist in the system.
Functions should be short and concise. If the code looks confusing and it would be clearer to write more lines
of code then do so! Check out the online awards for writing the most confusing C code! A good programmer
writes clean code that is easy to understand and maintain not the fewest lines of code!
Figure 2 – A short program
Jacob Beningo is a lecturer and consultant on embedded system design. He works with companies to
develop quality and robust products and overcome their embedded design challenges. Feel free to contact him
at [email protected] or at his website www.beningo.com.
If you liked this and would like to see a weekly collection of related products and features delivered directly
to your inbox, click here to sign up for the EDN on Systems Design newsletter.