DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 
Complying with standard C

Background

According to The C Programming Language, Kernighan and Ritchie, (First Edition), unsigned specified exactly one type; there were no unsigned chars, unsigned shorts, or unsigned longs, but most C compilers added these very soon thereafter. (Some compilers did not implement unsigned long but included the other two.) Naturally, implementations chose different rules for type promotions when these new types mixed with others in expressions.

The ANSI C compiler and most other C compilers used the simpler rule -- unsigned preserving. When an unsigned type needs to be widened, it is widened to an unsigned type; when an unsigned type mixes with a signed type, the result is an unsigned type.

The other rule, specified by ANSI C, came to be called value preserving, in which the result type depends on the relative sizes of the operand types. When an unsigned char or unsigned short is ``widened,'' the result type is int if an int is large enough to represent all the values of the smaller type. Otherwise the result type would be an unsigned int. The value preserving, rule produces the ``least surprise'' arithmetic result for most expressions.


Next topic: Compilation behavior
Previous topic: Promotions: unsigned vs. value preserving

© 2003 Caldera International, Inc. All rights reserved.
SCO OpenServer Release 5.0.7 -- 11 February 2003