Printing an unsigned char

Discussion in 'C Programming' started by Enrico `Trippo' Porreca, Jun 4, 2004.

  1. Suppose I wanted to print an unsigned char (in hex). Should I say:

    unsigned char x = 0x12;

    printf("%X\n", x);

    or

    printf("%X\n", (unsigned) x);

    ?

    If understand it correctly, x is converted to int (if int has a suitable
    range of values), because I'm passing it to a variadic function.

    I read different words in my C89 and C99 drafts: in the former, "%X"
    wants an int, while in the latter it needs an unsigned int.

    So the is cast needed in C99? If so, will the program be illegal in C89?

    --
    Enrico `Trippo' Porreca
    Enrico `Trippo' Porreca, Jun 4, 2004
    #1
    1. Advertising

  2. On Fri, 04 Jun 2004 18:27:49 +0200, Enrico `Trippo' Porreca
    <> wrote:

    >I read different words in my C89 and C99 drafts: in the former, "%X"
    >wants an int, while in the latter it needs an unsigned int.


    My copy of C89 states on page 133 that "%X" takes an unsigned int
    argument. The C99 Standard makes the same claim. Where did you see
    that "%X" takes an int in C89?

    Best wishes,

    Bob
    Robert W Hand, Jun 4, 2004
    #2
    1. Advertising

  3. Robert W Hand wrote:
    > On Fri, 04 Jun 2004 18:27:49 +0200, Enrico `Trippo' Porreca
    > <> wrote:
    >
    >>I read different words in my C89 and C99 drafts: in the former, "%X"
    >>wants an int, while in the latter it needs an unsigned int.

    >
    > My copy of C89 states on page 133 that "%X" takes an unsigned int
    > argument. The C99 Standard makes the same claim. Where did you see
    > that "%X" takes an int in C89?


    In the C89 draft taken from http://cern.ch/dan.pop/ansi.c I read:

    The conversion specifiers and their meanings are

    d, i, o, u, x, X The int argument is converted to signed decimal ( d
    or i ), unsigned octal ( o ), unsigned decimal ( u ), or unsigned
    hexadecimal notation ( x or X ); the letters abcdef are used for x
    conversion and the letters ABCDEF for X conversion.

    Maybe it was changed in the actual standard?

    --
    Enrico `Trippo' Porreca
    Enrico `Trippo' Porreca, Jun 4, 2004
    #3
  4. On Fri, 04 Jun 2004 23:27:29 +0200, Enrico `Trippo' Porreca wrote:


    > In the C89 draft taken from http://cern.ch/dan.pop/ansi.c I read:
    >
    > The conversion specifiers and their meanings are
    >
    > d, i, o, u, x, X The int argument is converted to signed decimal ( d
    > or i ), unsigned octal ( o ), unsigned decimal ( u ), or unsigned
    > hexadecimal notation ( x or X ); the letters abcdef are used for x
    > conversion and the letters ABCDEF for X conversion.
    >
    > Maybe it was changed in the actual standard?

    Point on. "or unsigned hexadecimal notation ( x or X )".
    Means x and X are unsigned int.
    =?iso-8859-1?q?Nils_O=2E_Sel=E5sdal?=, Jun 5, 2004
    #4
  5. Enrico `Trippo' Porreca

    Joe Wright Guest

    Nils O. SelÄsdal wrote:

    > On Fri, 04 Jun 2004 23:27:29 +0200, Enrico `Trippo' Porreca wrote:
    >
    >
    >
    >>In the C89 draft taken from http://cern.ch/dan.pop/ansi.c I read:
    >>
    >> The conversion specifiers and their meanings are
    >>
    >> d, i, o, u, x, X The int argument is converted to signed decimal ( d
    >> or i ), unsigned octal ( o ), unsigned decimal ( u ), or unsigned
    >> hexadecimal notation ( x or X ); the letters abcdef are used for x
    >> conversion and the letters ABCDEF for X conversion.
    >>
    >>Maybe it was changed in the actual standard?

    >
    > Point on. "or unsigned hexadecimal notation ( x or X )".
    > Means x and X are unsigned int.


    Reading impaired? It says the int argument corresponding to the 'x'
    or 'X' conversion specifiers is converted to unsigned.

    Regard..

    #include <stdio.h>

    int main(void) {
    int x = -2;
    printf("%#x\n", x);
    return 0;
    }

    ...prints 0xfffffffe

    --
    Joe Wright mailto:
    "Everything should be made as simple as possible, but not simpler."
    --- Albert Einstein ---
    Joe Wright, Jun 5, 2004
    #5
  6. Enrico `Trippo' Porreca

    Dan Pop Guest

    In <> Enrico `Trippo' Porreca <> writes:

    >Robert W Hand wrote:
    >> On Fri, 04 Jun 2004 18:27:49 +0200, Enrico `Trippo' Porreca
    >> <> wrote:
    >>
    >>>I read different words in my C89 and C99 drafts: in the former, "%X"
    >>>wants an int, while in the latter it needs an unsigned int.

    >>
    >> My copy of C89 states on page 133 that "%X" takes an unsigned int
    >> argument. The C99 Standard makes the same claim. Where did you see
    >> that "%X" takes an int in C89?

    >
    >In the C89 draft taken from http://cern.ch/dan.pop/ansi.c I read:
    >
    > The conversion specifiers and their meanings are
    >
    > d, i, o, u, x, X The int argument is converted to signed decimal ( d
    > or i ), unsigned octal ( o ), unsigned decimal ( u ), or unsigned
    > hexadecimal notation ( x or X ); the letters abcdef are used for x
    > conversion and the letters ABCDEF for X conversion.
    >
    >Maybe it was changed in the actual standard?


    Yes, this is one of the changes between the last public draft and the
    final C89 standard (K&R2 still needs to be fixed). The other I can
    remember right now is CLK_TCK being replaced by CLOCKS_PER_SEC.

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
    Dan Pop, Jun 7, 2004
    #6
  7. Enrico `Trippo' Porreca

    Dan Pop Guest

    In <> Enrico `Trippo' Porreca <> writes:

    >Suppose I wanted to print an unsigned char (in hex). Should I say:
    >
    > unsigned char x = 0x12;
    >
    > printf("%X\n", x);
    >
    >or
    >
    > printf("%X\n", (unsigned) x);
    >
    >?
    >
    >If understand it correctly, x is converted to int (if int has a suitable
    >range of values), because I'm passing it to a variadic function.
    >
    >I read different words in my C89 and C99 drafts: in the former, "%X"
    >wants an int, while in the latter it needs an unsigned int.


    The final C89 standard has the same wording as the C99 draft: %X expects
    unsigned int.

    >So the is cast needed in C99?


    Not really, because in C99 you can use %hhX and leave x uncast.

    >If so, will the program be illegal in C89?


    The one without the cast, yes, if the standard is read at its strictest.
    The committee intent, as expressed in a non-normative footnote was to
    allow it, as long as the value of x is in the range of both types.

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
    Dan Pop, Jun 7, 2004
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steffen Fiksdal

    void*, char*, unsigned char*, signed char*

    Steffen Fiksdal, May 8, 2005, in forum: C Programming
    Replies:
    1
    Views:
    571
    Jack Klein
    May 9, 2005
  2. Junmin H.
    Replies:
    20
    Views:
    997
    Charlie Gordon
    Sep 20, 2007
  3. Ioannis Vranos
    Replies:
    11
    Views:
    747
    Ioannis Vranos
    Mar 28, 2008
  4. Ioannis Vranos

    Padding bits and char, unsigned char, signed char

    Ioannis Vranos, Mar 28, 2008, in forum: C Programming
    Replies:
    6
    Views:
    604
    Ben Bacarisse
    Mar 29, 2008
  5. Alex Vinokur
    Replies:
    9
    Views:
    773
    James Kanze
    Oct 13, 2008
Loading...

Share This Page