QUIRKS - possible surprises in ANSI C.

Certain aspects of ANSI C may cause unpleasant surprises for programmers. A number of these are described below. Full details are available in the C reference manual.

Widening Integers:

If an "unsigned short" or unsigned bit field is used in an expression, it is usually converted to the SIGNED "int" number with the same value. However, if the size (number of bits) in the "short" or bit field is the same as the size of an "int", the value is converted to "unsigned int".

This makes for inconsistent behavior when porting programs. For example, on a DPS-8 machine, "short" and "int" are the same size, so an "unsigned short" is always converted to "unsigned int". However, on a number of other machines, "short" is 16 bits and "int" is 32 bits, so an "unsigned short" is always converted to "signed int".

Casts vs. Function Calls:

Consider the following.

typedef X /*some type*/;
    ...
(X)(A,B,C)

This is interpreted as the comma expression (A,B,C), and the result of this expression is cast to the X type. If, however, you omit the "typedef" statement (accidentally), the expression is assumed to be a function call to some undeclared function X, with arguments A, B, and C. This can be a confusing result.

See Also:

expl c manual

Copyright © 1996, Bull HN and Thinkage Ltd.