help to explain sscanf()

Discussion in 'C Programming' started by BlueJ, Nov 5, 2007.

  1. BlueJ

    BlueJ Guest

    Hi,

    I run the following source code:

    #include <stdio.h>

    main()
    {
    int i;
    unsigned int j;
    char input[] = "10 0x1b aaaaaaaa bbbbbbb";
    char s[5];
    sscanf(input, "%d %x %5[a-z] %*s %f ", &i, &j, s, s);
    printf("%d %d %s\n", i, j, s);
    }


    And it prints out: 10 27 aaaaa

    What I understand is sscan puts something into s twice. the first
    time, s = "aaaaa". afterwards, sscanf put some floating number to s.
    So when printf, s should be not aaaa.

    can anybody help to explain?
    BlueJ, Nov 5, 2007
    #1
    1. Advertising

  2. BlueJ

    Mark Bluemel Guest

    BlueJ wrote:
    > Hi,
    >
    > I run the following source code:
    >
    > #include <stdio.h>
    >
    > main()
    > {
    > int i;
    > unsigned int j;
    > char input[] = "10 0x1b aaaaaaaa bbbbbbb";
    > char s[5];
    > sscanf(input, "%d %x %5[a-z] %*s %f ", &i, &j, s, s);
    > printf("%d %d %s\n", i, j, s);
    > }
    >
    >
    > And it prints out: 10 27 aaaaa
    >
    > What I understand is sscan puts something into s twice.


    Does it? Are you sure?

    > the first
    > time, s = "aaaaa". afterwards, sscanf put some floating number to s.




    > So when printf, s should be not aaaa.
    >
    > can anybody help to explain?
    >

    Read the documentation for scanf - what does the return value tell you?
    Mark Bluemel, Nov 5, 2007
    #2
    1. Advertising

  3. On Mon, 05 Nov 2007 02:35:43 -0800, BlueJ <> wrote:

    >Hi,
    >
    >I run the following source code:
    >
    >#include <stdio.h>
    >
    >main()
    >{
    > int i;
    > unsigned int j;
    > char input[] = "10 0x1b aaaaaaaa bbbbbbb";
    > char s[5];
    > sscanf(input, "%d %x %5[a-z] %*s %f ", &i, &j, s, s);


    What exactly do you think a conversion specification means %5[a-z]
    means? Did you perhaps want the two specifications to read %5[a-z]s
    %*s instead of %5[a-z] %*s, a single specification which has two %
    characters and an imbedded space?

    Which argument corresponds to the %f? Does it in fact point to a
    float?

    > printf("%d %d %s\n", i, j, s);


    What type of argument does the second %d require? What type did you
    give it?

    >}
    >
    >
    >And it prints out: 10 27 aaaaa
    >
    >What I understand is sscan puts something into s twice. the first
    >time, s = "aaaaa". afterwards, sscanf put some floating number to s.


    What type is s? Is it capable of holding a float?

    >So when printf, s should be not aaaa.
    >
    >can anybody help to explain?


    By definition, undefined behavior is unexplainable.


    Remove del for email
    Barry Schwarz, Nov 6, 2007
    #3
  4. Barry Schwarz <> writes:

    > On Mon, 05 Nov 2007 02:35:43 -0800, BlueJ <> wrote:
    >
    >>I run the following source code:
    >>
    >>#include <stdio.h>
    >>
    >>main()
    >>{
    >> int i;
    >> unsigned int j;
    >> char input[] = "10 0x1b aaaaaaaa bbbbbbb";
    >> char s[5];
    >> sscanf(input, "%d %x %5[a-z] %*s %f ", &i, &j, s, s);

    >
    > What exactly do you think a conversion specification means %5[a-z]
    > means? Did you perhaps want the two specifications to read %5[a-z]s
    > %*s instead of %5[a-z] %*s, a single specification which has two %
    > characters and an imbedded space?


    A %[ format ends at "the matching ]" which is the first ] unless the
    format starts [] or [^]. The original %5[a-z] is quite probably
    right.

    As you point out, the 's' argument supplied for the %f is wrong.

    --
    Ben.
    Ben Bacarisse, Nov 6, 2007
    #4
  5. On Tue, 06 Nov 2007 16:48:38 +0000, Ben Bacarisse
    <> wrote:

    >Barry Schwarz <> writes:
    >
    >> On Mon, 05 Nov 2007 02:35:43 -0800, BlueJ <> wrote:
    >>
    >>>I run the following source code:
    >>>
    >>>#include <stdio.h>
    >>>
    >>>main()
    >>>{
    >>> int i;
    >>> unsigned int j;
    >>> char input[] = "10 0x1b aaaaaaaa bbbbbbb";
    >>> char s[5];
    >>> sscanf(input, "%d %x %5[a-z] %*s %f ", &i, &j, s, s);

    >>
    >> What exactly do you think a conversion specification means %5[a-z]
    >> means? Did you perhaps want the two specifications to read %5[a-z]s
    >> %*s instead of %5[a-z] %*s, a single specification which has two %
    >> characters and an imbedded space?

    >
    >A %[ format ends at "the matching ]" which is the first ] unless the
    >format starts [] or [^]. The original %5[a-z] is quite probably
    >right.


    Yes. For some reason I had it in my mind that the [...] was a
    modifier when it is actually a specification. Oddly enough, n1124
    says the - makes the behavior implementation defined. I wonder if any
    system (other than the DS9000) does something different from the
    obvious.


    Remove del for email
    Barry Schwarz, Nov 8, 2007
    #5
  6. BlueJ

    Ben Pfaff Guest

    Barry Schwarz <> writes:

    >>> On Mon, 05 Nov 2007 02:35:43 -0800, BlueJ <> wrote:
    >>>> sscanf(input, "%d %x %5[a-z] %*s %f ", &i, &j, s, s);


    > Oddly enough, n1124 says the - makes the behavior
    > implementation defined. I wonder if any system (other than the
    > DS9000) does something different from the obvious.


    It's probably implementation-defined because the characters in
    between 'a' and 'z' are implementation-defined; 'a' might not
    even precede 'z'. Only ranges of digits could be portable.
    --
    char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[]
    ={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa67f6aaa,0xaa9aa9f6,0x11f6},*p
    =b,i=24;for(;p+=!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+
    2:{i++;if(i)break;else default:continue;if(0)case 1:putchar(a[i&15]);break;}}}
    Ben Pfaff, Nov 8, 2007
    #6
  7. Ben Pfaff <> writes:

    > Barry Schwarz <> writes:
    >
    >>>> On Mon, 05 Nov 2007 02:35:43 -0800, BlueJ <> wrote:
    >>>>> sscanf(input, "%d %x %5[a-z] %*s %f ", &i, &j, s, s);

    >
    >> Oddly enough, n1124 says the - makes the behavior
    >> implementation defined. I wonder if any system (other than the
    >> DS9000) does something different from the obvious.

    >
    > It's probably implementation-defined because the characters in
    > between 'a' and 'z' are implementation-defined; 'a' might not
    > even precede 'z'. Only ranges of digits could be portable.


    I think Barry Schwarz may be commenting on the extreme degree of
    latitude given. The text says:

    "If a - character is in the scanlist and is not the first, nor the
    second where the first character is a ^, nor the last character,
    the behavior is implementation-defined."

    So on a DS9000, %[a-z] can match digit sequences and %[z-a] can match
    UK postcodes. Equally, %[0-9] need not match digits given that
    definition (provided the implementation does say what it means).

    It would have been more helpful to insist that <x>-<y> denotes a range
    (from x to y) of characters determined by the implementation's
    character set (provided y >= x). Then %[0-9] would then be portable,
    and %[a-z] would mean the same on all implementations that use the same
    character set.

    --
    Ben.
    Ben Bacarisse, Nov 8, 2007
    #7
  8. "Ben Pfaff" <> a écrit dans le message de news:
    ...
    > Barry Schwarz <> writes:
    >
    >>>> On Mon, 05 Nov 2007 02:35:43 -0800, BlueJ <> wrote:
    >>>>> sscanf(input, "%d %x %5[a-z] %*s %f ", &i, &j, s, s);

    >
    >> Oddly enough, n1124 says the - makes the behavior
    >> implementation defined. I wonder if any system (other than the
    >> DS9000) does something different from the obvious.

    >
    > It's probably implementation-defined because the characters in
    > between 'a' and 'z' are implementation-defined; 'a' might not
    > even precede 'z'. Only ranges of digits could be portable.


    IIRC, a-z spans more than 26 values in EBCDIC.

    > --
    > char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long
    > b[]
    > ={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa67f6aaa,0xaa9aa9f6,0x11f6},*p
    > =b,i=24;for(;p+=!*p;*p/=4)switch(0[p]&3)case 0:{return
    > 0;for(p--;i--;i--)case+
    > 2:{i++;if(i)break;else default:continue;if(0)case
    > 1:putchar(a[i&15]);break;}}}


    Your signature compiles with a warning:
    bpsig.c:1: warning: control reaches end of non-void function

    This is a corrected version, smaller of course ;-)

    int putchar(int);int main(void){unsigned long b[]={0x67dffdff,0x9aa9aa6a
    ,0xa77ffda9,0x7da6aa6a,0xa67f6aaa,0xaa9aa9f6,4598},*p=b,i=24;for(;;p+=!(
    *p/=4))switch(0[p]&3)case-0:{return 0;while(i-=2)case+2:{if(!++i)default
    :continue;else break;case!0:putchar("\n .CJacehknorstu"[i&15]);break;}}}

    --
    Chqrlie.
    Charlie Gordon, Nov 8, 2007
    #8
    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. Anonymous
    Replies:
    0
    Views:
    478
    Anonymous
    Oct 30, 2003
  2. Andrej Hocevar
    Replies:
    1
    Views:
    479
    Nick Austin
    Aug 18, 2003
  3. Default User

    Help with sscanf()

    Default User, Feb 28, 2005, in forum: C Programming
    Replies:
    4
    Views:
    722
    Default User
    Mar 1, 2005
  4. Artemio

    Help with sscanf() needed desperately

    Artemio, Jul 29, 2006, in forum: C Programming
    Replies:
    8
    Views:
    336
    Default User
    Jul 30, 2006
  5. JoeT

    sscanf help

    JoeT, Aug 21, 2010, in forum: C Programming
    Replies:
    7
    Views:
    492
    David Thompson
    Sep 4, 2010
Loading...

Share This Page