Tips and Tricks – Learning to write portable code using ANSI-C

Embedded software engineers should be interested in writing production software that is portable and reusable.  Societal demands for refreshing embedded systems every 12 – 18 months are quite demanding and starting from scratch for many systems is just not realistic.  As developers we really should be interested in working on the cutting edge and not reinventing the wheel over and over and over again.  One step towards the goal of writing portable software is to learn and understand ANSI-C.

ANSIC

On a weekly basis I am blessed with the opportunity to interact with and help hundreds of embedded software developers through-out the world.  I’ve noticed that as a group, only about 1 in 25 of us has actually read through main portions of the ANSI-C standard. Another 1 in 25 have at least referred to the standard at one point or another.  The other 23 of 25 have never even picked up the standard!

The ANSI-C standard spells out the constructs and tools available to developers using the C language.  The standard provides the toolbox and the description of those tools and how they can be used.  One of the first steps to learning how to write portable software is to pick up the standard and read through it.  There is no reason to read the standard in one sitting but reading a few sections a week and thinking through their application can have a profound effect on understanding how to use the C language.

Once a developer has read through the standard they have the foundational understanding of what constructs are in the standard.  Writing code with any construct outside the standard whether it be a compiler intrinsic or language add-on will be a sticking point to the portability of the software.  The goal will be to avoid non-standard constructs.

Reading through the standard will also allow a developer to gain insights into the areas of the standard that aren’t fully defined for compiler vendors.  Take for example the size of int or structures and bit fields.  The specification of these constructs are ambiguous and once understood the goal will be to avoid their use or at least document why they are required in a certain application.

Writing portable software isn’t trivial and requires many areas of expertise but the first and possibly the most foundational is understanding the ANSI-C standard.  Have you read it?  If not, you may want to start reading through it one bit at a time.

Share >

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.