[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: printf ("%hhd", 0x100);
From: |
Mathieu Lacage |
Subject: |
Re: printf ("%hhd", 0x100); |
Date: |
Fri, 16 Jan 2004 22:04:50 +0100 |
On Fri, 2004-01-16 at 12:26, Rainer Vaaßen wrote:
> > I believe the following code should generate twice the same output on
> > any architecture.
> >
> > #include <stdio.h>
> > int main (int argc, char *argv[])
> > {
> > signed char sc = (signed char) 0x100;
> > printf ("%hhd\n", sc);
> > printf ("%hhd\n", 0x100);
> > return 0;
> > }
> >
> > However, on my system, I get the following:
> > address@hidden stdio-common]$ ./a.out
> > 0
> > 256
> > address@hidden stdio-common]$
>
> hi,
> the range for char is 0 - 255 and not 256
Yes, this is true. However, I fail to see how this relates to my
question. Maybe I should have explained why I believe this output is
surprising.
Have you read the relevant sections of the C99 standard for the printf
function conversion specifiers (7.19.6.1, p273) ? As far as I can tell,
it says printf is supposed to do a signed char cast for %hhd conversions
(which is what the first 2 lines of the main function shown above do)
which printf does not do here obviously.
The current glibc (I am using RH9's glibc 2.3.2-11.9) implementation
gives the expected output for %hhu but not for %hhd as shown bellow:
#include <stdio.h>
int main (int argc, char *argv[])
{
printf ("%hhd\n", 0x100);
printf ("%hhu\n", 0x100);
return 0;
}
address@hidden mathieu]$ ./a.out
256
0
address@hidden mathieu]$
I fail to see how the output above can be standard compliant.
regards,
Mathieu
--
Mathieu Lacage <address@hidden>
- printf ("%hhd", 0x100);, Mathieu Lacage, 2004/01/15
- Message not available
- Re: printf ("%hhd", 0x100);,
Mathieu Lacage <=