ccharplatform-independent

Avoid implementation defined types in platform independent code


I just started working on a big project which is supposed to be platform independent, but it actually uses types which are implementation defined such as char.

This had already caused some problems, since for some devices char is by default signed while for others unsigned.

I would like to find a solution in order to avoid the use of implementation defined types in code that must be platform independent.

  1. What would be the best solution? Re-defining all the char variables as unsigned char (or signed char), using compiler parameters (ex: -funsigned-char), typedefs, or others?
  2. Are there other types for which the standard does not define if they are either signed or unsigned?

Solution

  • In theory, the answer is very simple:

    Always use types for the purpose they are intended for. E.g. you want implementation-defined types for something like the size of an array because on different platforms, the upper bound of such a size will be different. Similar for pointers. For the need of fixed-size types, C already provides appropriate typedefs in stdint.h.

    Here's an incomplete "what-to-use" list: