Writing a portable C program - which things to consider?

helpermethod picture helpermethod · Feb 20, 2010 · Viewed 9.7k times · Source

For a project at university I need to extend an existing C application, which shall in the end run on a wide variety of commercial and non-commercial unix systems (FreeBSD, Solaris, AIX, etc.).

Which things do I have to consider when I want to write a C program which is most portable?

Answer

EvilTeach picture EvilTeach · Feb 20, 2010

The best advice I can give, is to move to a different platform every day, testing as you go.
This will make the platform differences stick out like a sore thumb, and teach you the portability issues at the same time.

Saving the cross platform testing for the end, will lead to failure.

That aside

  • Integer sizes can vary.
  • floating point numbers might be represented differently.
  • integers can have different endianism.
  • Compilation options can vary.
  • include file names can vary.
  • bit field implementations will vary.

It is generally a good idea to set your compiler warning level up as high as possible, to see the sorts of things the compiler can complain about.