Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing a portable C program - which things to consider?

Tags:

c

portability

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?

like image 673
helpermethod Avatar asked Feb 20 '10 20:02

helpermethod


People also ask

Why is C considered portable?

C is a portable programming language Because it is not tied to any hardware or system. We can say, it is a hardware independent language or platform independent language. That is why c is called a portable language.

What is needed to write a basic C?

Aside from a compiler, the only other software requirement is a text editor for writing and saving your C code.

What is required in which C program?

The correct answer to the question “What is required in each C program” is, option (a). The program must have at least one function. Any C program will have a function, and function is nothing but a piece of code.


2 Answers

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.

like image 52
EvilTeach Avatar answered Oct 02 '22 16:10

EvilTeach


I used to write C utilities that I would then support on 16 bit to 64 bit architectures, including some 60 bit machines. They included at least three varieties of "endianness," different floating point formats, different character encodings, and different operating systems (though Unix predominated).

  1. Stay as close to standard C as you can. For functions/libraries not part of the standard, use as widely supported a code base as you can find. For example, for networking, use the BSD socket interface, with zero or minimal use of low level socket options, out-of-band signalling, etc. To support a wide number of disparate platforms with minimal staff, you'll have to stay with plain vanilla functions.
  2. Be very aware of what's guaranteed by the standard, vice what's typical implementation behavior. For instance, pointers are not necessarily the same size as integers, and pointers to different data types may have different lengths. If you must make implementation dependent assumptions, document them thoroghly. Lint, or --strict, or whatever your development toolset has as an equivalent, is vitally important here.
  3. Header files are your friend. Use implementaton defined macros and constants. Use header definitions and #ifdef to help isolate those instances where you need to cover a small number of alternatives.
  4. Don't assume the current platform uses EBCDIC characters and packed decimal integers. There are a fair number of ASCII - two's complement machines out there as well. :-)

With all that, if you avoid the tempation to write things multiple times and #ifdef major portions of code, you'll find that coding and testing across disparate platforms helps find bugs sooner. You'll end up producing more disciplined, understandable, maintainable programs.

like image 45
mpez0 Avatar answered Oct 02 '22 15:10

mpez0