sscanf error for unsign int type.

Discussion in 'C Programming' started by loudking, Dec 4, 2007.

  1. loudking

    loudking Guest

    Hi all,

    I encountered a problem for sscanf from a string for unsigned int
    type. The warning message is

    main.c: In function `main':
    main.c:171: warning: unsigned int format, different type arg (arg 3)

    =========================================================
    if (begin_scn) {
    u_short scn_major;
    u_int scn_minor;

    if (sscanf(begin_scn, "%4x%*1[.:,]%8x", &scn_major,
    &scn_minor)
    != 2) {
    fprintf(stderr, "Wrong SCN format: %s\n", begin_scn);
    return 1;
    }
    }
    =========================================================

    Would anybody tell me how to solve this problem?

    Thanks in advance!
     
    loudking, Dec 4, 2007
    #1
    1. Advertising

  2. loudking

    santosh Guest

    loudking wrote:

    > Hi all,
    >
    > I encountered a problem for sscanf from a string for unsigned int
    > type. The warning message is
    >
    > main.c: In function `main':
    > main.c:171: warning: unsigned int format, different type arg (arg 3)
    >
    > =========================================================
    > if (begin_scn) {
    > u_short scn_major;
    > u_int scn_minor;
    >
    > if (sscanf(begin_scn, "%4x%*1[.:,]%8x", &scn_major,
    > &scn_minor)
    > != 2) {
    > fprintf(stderr, "Wrong SCN format: %s\n", begin_scn);
    > return 1;
    > }
    > }
    > =========================================================
    >
    > Would anybody tell me how to solve this problem?
    >
    > Thanks in advance!


    Use the 'u' format specifier for unsigned int and 'hu' for unsigned
    short.
     
    santosh, Dec 4, 2007
    #2
    1. Advertising

  3. "loudking" <> schrieb im Newsbeitrag
    news:...
    > Hi all,
    >
    > I encountered a problem for sscanf from a string for unsigned int
    > type. The warning message is
    >
    > main.c: In function `main':
    > main.c:171: warning: unsigned int format, different type arg (arg 3)
    >
    > =========================================================
    > if (begin_scn) {
    > u_short scn_major;
    > u_int scn_minor;
    >
    > if (sscanf(begin_scn, "%4x%*1[.:,]%8x", &scn_major,
    > &scn_minor)
    > != 2) {
    > fprintf(stderr, "Wrong SCN format: %s\n", begin_scn);
    > return 1;
    > }
    > }
    > =========================================================
    >
    > Would anybody tell me how to solve this problem?
    >
    > Thanks in advance!

    Apparently ip complains about scn_major not being an insigned int, while
    scanf'c %X expects it to be just that. Try %hx for unsigned short.

    Bye, Jojo
     
    Joachim Schmitz, Dec 4, 2007
    #3
  4. "Joachim Schmitz" <> schrieb im Newsbeitrag
    news:fj3pkt$ab4$...
    > "loudking" <> schrieb im Newsbeitrag
    > news:...
    >> Hi all,
    >>
    >> I encountered a problem for sscanf from a string for unsigned int
    >> type. The warning message is
    >>
    >> main.c: In function `main':
    >> main.c:171: warning: unsigned int format, different type arg (arg 3)
    >>
    >> =========================================================
    >> if (begin_scn) {
    >> u_short scn_major;
    >> u_int scn_minor;
    >>
    >> if (sscanf(begin_scn, "%4x%*1[.:,]%8x", &scn_major,
    >> &scn_minor)
    >> != 2) {
    >> fprintf(stderr, "Wrong SCN format: %s\n", begin_scn);
    >> return 1;
    >> }
    >> }
    >> =========================================================
    >>
    >> Would anybody tell me how to solve this problem?
    >>
    >> Thanks in advance!

    > Apparently ip complains about scn_major not being an insigned int, while
    > scanf'c %X expects it to be just that. Try %hx for unsigned short.

    Guess I'd better spellcheck _before_ sending...
    "Apparently it complains about scn_major not being an unsigned int, while
    scanf's %x expects it to be just that."

    Bye, Jojo
     
    Joachim Schmitz, Dec 4, 2007
    #4
  5. loudking wrote:

    > main.c: In function `main':
    > main.c:171: warning: unsigned int format, different type arg (arg 3)


    > if (begin_scn) {
    > u_short scn_major;
    > u_int scn_minor;
    >
    > if (sscanf(begin_scn, "%4x%*1[.:,]%8x", &scn_major,
    > &scn_minor)
    > != 2) {
    > fprintf(stderr, "Wrong SCN format: %s\n", begin_scn);
    > return 1;
    > }
    > }


    > Would anybody tell me how to solve this problem?


    Note the format specifier below:

    #include <stdio.h>
    #include <stdlib.h>

    int main(void)
    {
    char begin_scn[] = "00ab.721a00ac";
    unsigned short scn_major;
    unsigned int scn_minor;

    if (sscanf(begin_scn, "%4hx%*1[.:,]%8x", &scn_major, &scn_minor)
    != 2) {
    fprintf(stderr, "Wrong SCN format: %s\n", begin_scn);
    return EXIT_FAILURE;
    }
    printf("begin_scn is \"%s\"\n"
    "scn_major = %#x\n"
    "scn_minor = %#x\n", begin_scn, scn_major, scn_minor);
    return 0;
    }

    [output]
    begin_scn is "00ab.721a00ac"
    scn_major = 0xab
    scn_minor = 0x721a00ac
     
    Martin Ambuhl, Dec 4, 2007
    #5
    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. Schnoffos
    Replies:
    2
    Views:
    1,221
    Martien Verbruggen
    Jun 27, 2003
  2. Hal Styli
    Replies:
    14
    Views:
    1,648
    Old Wolf
    Jan 20, 2004
  3. Joerg Schwerdtfeger

    printf / sscanf unsigned long long int variables

    Joerg Schwerdtfeger, Apr 2, 2004, in forum: C Programming
    Replies:
    3
    Views:
    40,354
    Joerg Schwerdtfeger
    Apr 2, 2004
  4. powerhouse04
    Replies:
    1
    Views:
    323
    powerhouse04
    Dec 16, 2007
  5. Hans Vlems

    long long int and sscanf, printf

    Hans Vlems, Sep 7, 2012, in forum: C Programming
    Replies:
    5
    Views:
    1,572
    Hans Vlems
    Sep 7, 2012
Loading...

Share This Page