INF: C 6.0 sizeof() Function Returns unsigned Instead of int

ID Number: Q60332

6.00 6.00a 6.00ax 7.00 | 6.00 6.00a

MS-DOS | OS/2

Summary:

To comply with new ANSI specifications, the sizeof() operator in

Microsoft C version 6.0 and later and QuickC version 2.5 and later

returns an unsigned integer. In C version 5.1 and QuickC version 2.0,

this function returns a signed int.

More Information:

The unsigned return value can cause problems in cases using

-sizeof(type). In the following example, trying to position a file one

record from the end, which worked properly under C version 5.1, does

not work properly under C version 6.0:

fseek(file,(long)(-sizeof(record)),SEEK_END);

This now gives a location far beyond the file's end because the

generated value is no longer sign extended. Since the unary minus (-)

doesn't change the "signedness" of a variable and the sizeof()

operator is now an unsigned int, the compiler performs a zero

extension to convert from an unsigned int to a long. This change in

representation of the sizeof() operator was done to maintain

compatibility with the ANSI standard.

In other words, if "record" above is of type char, instead of

returning a long value of -1, the following returns the positive value

of 0x0000ffff or 32767:

(long)(-sizeof(record))

One workaround is to cast the sizeof() result to a signed int

before casting it to a long. For example:

(long)-(int)sizeof(record)

Additional reference words: 5.10 6.00 6.00a 6.00ax 7.00