EmbeddedRelated.com
Forums

Delay Routine: Fully-portable C89 if possible

Started by Martin Wells October 9, 2007
CBFalconer wrote:
> David Brown wrote: > ... snip ... >> I don't know what you mean by this. If I want the smallest type >> that holds 16-bit signed integers, I use a sint16_t - that's >> portable, and gives me exactly what I want. > > There is no such thing as a 'sint16_t', except as a non-standard > extension in some system or other. The following extracts are from > N869. > > 7.8 Format conversion of integer types <inttypes.h> > > [#1] The header <inttypes.h> includes the header <stdint.h> > and extends it with additional facilities provided by hosted > implementations. > > [#2] It declares four functions for converting numeric > character strings to greatest-width integers and, for each > type declared in <stdint.h>, it defines corresponding macros > for conversion specifiers for use with the formatted > input/output functions.170) > > Forward references: integer types <stdint.h> (7.18). > > and > > 7.18.1.1 Exact-width integer types > > [#1] The typedef name intN_t designates a signed integer | > type with width N. Thus, int8_t denotes a signed integer | > type with a width of exactly 8 bits. > > [#2] The typedef name uintN_t designates an unsigned integer | > type with width N. Thus, uint24_t denotes an unsigned | > integer type with a width of exactly 24 bits. | > > [#3] These types are optional. However, if an | > implementation provides integer types with widths of 8, 16, | > 32, or 64 bits, it shall define the corresponding typedef | > names. > > Notice the above paragraph 3, stating these types are _optional_. > Also note that all this is not present in the C90 (or C95) > standard. > > However the thing that is universally known, and not optional, is > that an int (and a short int) has a minimum size of 16 bits, and > that a long has a minimum size of 32 bits. If a compiler fails to > meet these values it is non-compliant, and does not deserve to be > called a C compiler. >
The "sint16_t" was a typo - it should of course be "int16_t". For a C99 compiler, "int16_t" is *not* optional unless there are no 16-bit integer types available on the implementation. Thus int16_t is standard and portable on modern compilers (many non-C99 compilers provide "cheap" C99 features, such as // comments and <stdint.h>). You are perfectly correct in saying that a compiler whose "int" is less that 16-bit does not implement the C language correctly (never mind any of the standards). However, the fact remains that in the embedded market there are "C" compilers that target 8-bit cpus, have 8-bit ints (at least as an option), and are useful and productive tools despite not technically being "C".