This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Patch for making USHRT_MAX of type unsigned int



Vladimir Makarov <vmakarov@toke.toronto.redhat.com> writes:

> Hello,
> 
>   According to ANSI C standard, compiler defines type of decimal
> constant without suffix searching appropriate type in the following
> order:
>        int
>        long int
>        long int int
> 
>   What type will integer constant 65535 have on a 16-bit machine with
> short int and int are represented by 2 byte integer and long int is
> represented by 4 byte integer?  It will be long int.  In this case we
> have
> 
> sizeof (USHRT_MAX) == sizeof (long) == 4
> sizeof (USHRT_MAX) > sizeof (UINT_MAX)
> 
> which is not what the most users expect.  The following patch solves
> the problem.  The unsigned suffix for the UCHAR_MAX, CHAR_MAX is added
> for uniformity.
> 
>   Actually, I did not find that sizeof (USHRT_MAX) > sizeof (UINT_MAX)
> contradicts to the standard.  Therefore I am asking the approval to
> commit the patch or waiting for any comment.

This patch is not quite right.

The C standard says that types such as USHRT_MAX should have type `as
would an expression that is an object of the corresponding type
converted according to the integer promotions'.

So these values should be of type 'int' if 'int' can hold them, or
'unsigned int' if not (I believe one of the two is required to be able
to hold the values).

I would suggest

#if INT_MAX <= 65535
#define USHRT_MAX 65535
#else
#define USHRT_MAX 65535U
#endif

For UCHAR_MAX, it is always correct to just write

#define UCHAR_MAX 255

as 'int' must be at least 16 bits.

-- 
- Geoffrey Keating <geoffk@geoffk.org>


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]