what's the different between the %x and %c

Discussion in 'C Programming' started by zhangsonglovexiaoniuniu@gmail.com, Nov 23, 2007.

  1. Guest

    Hi all,

    i got a program as follows:

    void str2mac(void)
    {
    unsigned char xx[6];
    char str[20] = "11:22:33:44:55:66";

    /* load str to xx */
    1. sscanf(str,"%x:%x:%x:%x:%x:
    %x",&xx[0],&xx[1],&xx[2],&xx[3],&xx[4],&xx[5]);

    2. sscanf(str,"%c:%c:%c:%c:%c:
    %c",&xx[0],&xx[1],&xx[2],&xx[3],&xx[4],&xx[5]);
    }

    is there some difference between 1 and 2?


    thanks.
    Evan
    , Nov 23, 2007
    #1
    1. Advertising

  2. touchskyer Guest

    why not run it in your local machine? And you will see the difference.

    %c is for character
    %x is for hex

    scanf("%x", &x);
    >>124

    x=124
    scanf("%c", &c);
    >>124

    c='1'

    On 11ÔÂ23ÈÕ, ÏÂÎç5ʱ15·Ö, ""
    <> wrote:
    > Hi all,
    >
    > i got a program as follows:
    >
    > void str2mac(void)
    > {
    > unsigned char xx[6];
    > char str[20] = "11:22:33:44:55:66";
    >
    > /* load str to xx */
    > 1. sscanf(str,"%x:%x:%x:%x:%x:
    > %x",&xx[0],&xx[1],&xx[2],&xx[3],&xx[4],&xx[5]);
    >
    > 2. sscanf(str,"%c:%c:%c:%c:%c:
    > %c",&xx[0],&xx[1],&xx[2],&xx[3],&xx[4],&xx[5]);
    >
    > }
    >
    > is there some difference between 1 and 2?
    >
    > thanks.
    > Evan
    touchskyer, Nov 23, 2007
    #2
    1. Advertising

  3. Guest

    On Nov 23, 5:26 pm, touchskyer <> wrote:
    > why not run it in your local machine? And you will see the difference.
    >
    > %c is for character
    > %x is for hex
    >
    > scanf("%x", &x);>>124
    >
    > x=124
    > scanf("%c", &c);>>124
    >
    > c='1'
    >
    > On 11ÔÂ23ÈÕ, ÏÂÎç5ʱ15·Ö, ""
    >
    >
    >
    > <> wrote:
    > > Hi all,

    >
    > > i got a program as follows:

    >
    > > void str2mac(void)
    > > {
    > > unsigned char xx[6];
    > > char str[20] = "11:22:33:44:55:66";

    >
    > > /* load str to xx */
    > > 1. sscanf(str,"%x:%x:%x:%x:%x:
    > > %x",&xx[0],&xx[1],&xx[2],&xx[3],&xx[4],&xx[5]);

    >
    > > 2. sscanf(str,"%c:%c:%c:%c:%c:
    > > %c",&xx[0],&xx[1],&xx[2],&xx[3],&xx[4],&xx[5]);

    >
    > > }

    >
    > > is there some difference between 1 and 2?

    >
    > > thanks.
    > > Evan- Hide quoted text -

    >
    > - Show quoted text -



    sorry , i suddenly mind open. i know where i think wrong.
    thanks for the god and you.
    , Nov 23, 2007
    #3
  4. On Fri, 23 Nov 2007 01:15:33 -0800 (PST),
    ""
    <> wrote:

    > unsigned char xx[6];
    > char str[20] = "11:22:33:44:55:66";
    >
    > /* load str to xx */
    > 1. sscanf(str,"%x:%x:%x:%x:%x:
    > %x",&xx[0],&xx[1],&xx[2],&xx[3],&xx[4],&xx[5]);
    >

    Doesn't work reliably; %x expects an unsigned _int_ to store into, not
    a u-char. You might appear to luck out if your (current) machine and C
    implementation supports little-endian unaligned (u-)ints, and xx
    happens to be followed by something whose being clobbered doesn't
    (detectably) cause trouble; the former is true on a certain widely
    used architecture, and the latter is not too unlikely.

    In C99 (or with at least this C99-compatible extension to C90) you can
    use %hhx. Otherwise you need to store into at least u-shorts, and most
    convenient u-ints, and then copy elementwise.

    > 2. sscanf(str,"%c:%c:%c:%c:%c:
    > %c",&xx[0],&xx[1],&xx[2],&xx[3],&xx[4],&xx[5]);


    Doesn't work at all; (at best) it takes only the first character of
    the first piece, and then looks for the first colon and (usually)
    fails. Formally %c expects (pointer to) _plain_ char not unsigned
    char, but only unrealistically picky implementations care about this.
    Especially not implementations where plain char 'tastes' unsigned, as
    most (but not all) do nowadays.

    - formerly david.thompson1 || achar(64) || worldnet.att.net
    David Thompson, Dec 10, 2007
    #4
    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. jakk
    Replies:
    4
    Views:
    12,114
  2. Flip
    Replies:
    0
    Views:
    300
  3. Santa
    Replies:
    1
    Views:
    1,072
    Mark A. Odell
    Jul 17, 2003
  4. Joona I Palaste
    Replies:
    14
    Views:
    1,434
    Fred Bloggs
    Jan 20, 2004
  5. PerlFAQ Server
    Replies:
    0
    Views:
    353
    PerlFAQ Server
    Jan 6, 2011
Loading...

Share This Page