Re: Delay Routine: Fully-portable C89 if possible



CBFalconer wrote:
David Brown wrote:
... snip ...
Secondly, I was suggesting that if you want portable code, you have
to use size-specific integer types. Using <stdint.h> is an easy
way to get that - otherwise, a common format header file that is
adapted for the compiler/target in question is a useful method. It doesn't really matter whether you use "uint32_t" from <stdint.h>,
or have a "typedef unsigned long int uint32_t" in a common header
file - nor does it matter if you give the type your own name. But
it *does* matter that you have such types available in your code.

I disagree. "unsigned long" is guaranteed to have at least 32
bits. Similarly "unsigned int" is guaranteed at least 16 bits, as
is "unsigned short". "unsigned char" is at least 8 bits. These
types are ALWAYS available, but the various 'uint32_t' etc. types
are not. Even if present, they are only present on a C99 compiler
system. Why abandon portability unnecessarily?


As you say, an "unsigned long int" is guaranteed (by any strictly compliant compiler) to have *at least* 32 bits. But nowhere is it specified to have *at most* 32 bits. Thus "unsigned long int" is useless if I want *exactly* 32 bits. That is why the <stdint.h> types were defined, and that is why they are important if you want portable code that works on a wide range of targets (at least, for architectures that can support 8, 16 and 32-bit data - targets with 36 bit cpus, for example, will always be problematic for portable code). When you are using compilers without <stdint.h>, you need to write your own header with suitable typedefs - then you can keep your source code portable.
.