Re: Printf question.



On Fri, 30 Dec 2005 20:56:02 GMT, Keith Thompson <kst-u@xxxxxxx> wrote
in comp.lang.c:

> Jack Klein <jackklein@xxxxxxxxxxx> writes:
> > On 30 Dec 2005 11:21:25 -0800, "praeiji" <spacecaps@xxxxxxxxx> wrote
> > in comp.lang.c:
> [...]
> >> unsigned char a = 200;
> >> char b = 200;
> >> printf( "%d %d", a, b );
> >>
> >> gives :
> >>
> >> 200, -56
> >>
> >> How comes? I didn't tell printf that the first argument was unsigned
> >> and it detected it on its own. It doesn't seem possible with varargs.
> >> How is it possible?
> [snip]
>
> > In the case of integer types of lesser rank than int, the "integer
> > promotions" occur. Since a signed int can hold the value 200, the
> > value in the unsigned char 'a' is converted to the int value 200.
>
> Correction: since a signed int can hold any value of type unsigned
> char, the value in the unsigned char 'a' is promoted to type int. The
> promoted type doesn't depend on the value that happens to be in the
> variable; it depends on which type can hold *any* possible value of
> the type.

Yes, thanks.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
.



Relevant Pages

  • Re: The difference between signed and unsigned when doing `mod
    ... 'a' and 'b' will be promoted to signed int for the subtraction. ... When dealing with promotions of unsigned integer types to higher ... values of the higher types. ...
    (comp.lang.c)
  • Re: The difference between signed and unsigned when doing `mod
    ... Thanks to Walter Roberson although you're strict. ... 'a' and 'b' will be promoted to signed int for the subtraction. ... these promotions sometimes result in signed ... > values of the higher types. ...
    (comp.lang.c)
  • Re: printf("%f") question
    ... On most systems, unsigned char will be promoted to signed int, which ... existing implementations -- get away with printing a signed int ... signed char definitely promotes to int, ... been promoted according to the integer promotions, ...
    (comp.lang.c)
  • Re: printf("%f") question
    ... On most systems, unsigned char will be promoted to signed int, which ... signed char definitely promotes to int, ... been promoted according to the integer promotions, ...
    (comp.lang.c)
  • Re: printf("%f") question
    ... On most systems, unsigned char will be promoted to signed int, which should be printed using %d. ... You can usually -- probably on all existing implementations -- get away with printing a signed int using %u, or an unsigned int using %d, so long as the value is within the common range, but it's not correct. ... been promoted according to the integer promotions, ... be converted to signed char or unsigned char before printing" (C99 ...
    (comp.lang.c)