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

Third example: integral constants

In the following code, assume ints are 16 bits.

   int f(void)
   {
       int i = 0;
   

return i > 0xffff; }

Because the hexadecimal constant's type is either int (with a value of -1 on a two's-complement machine) or an unsigned int (with a value of 65535), the comparison will be true on a non-ANSI C compiler (-Xt and -Xk modes), and false on an ANSI C compiler (-Xa, -Xc, and -Xm modes).

An appropriate cast clarifies the code and silences the ANSI C compiler:

Non-ANSI C behavior:

   i > (int)0xffff
ANSI C behavior:
   i > (unsigned int)0xffff
          or
   i > 0xffffU
(The U suffix character is a new feature of ANSI C and will probably produce an error message with older compilers.)

Next topic: Tokenization and preprocessing
Previous topic: Integral constants

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