sscanf behavoir

Discussion in 'C Programming' started by Joachim Schmitz, Apr 19, 2007.

  1. Hi folks

    What would be the expected and correct output of the following

    #include <stdio.h>

    int main (void)
    {
    char buffer1[] = "(10,20,30)";
    char buffer2[] = "(10,20,30 )"; /* add two white spaces between 30
    and ) */
    int var1, var2, var3;
    int i = 11111;
    int j = 22222;
    sscanf (buffer1,"(%d,%d,%d)%n",&var1,&var2,&var3,&i);
    printf ("%s -> %d\n",buffer1,i);

    sscanf (buffer2,"(%d,%d,%d)%n",&var1,&var2,&var3,&j);
    printf ("%s -> %d\n",buffer2,j);

    return 0;
    }

    On one platform I get:
    (10,20,30) -> 10
    (10,20,30 ) -> 12

    on another I get:
    (10,20,30) -> 10
    (10,20,30 ) -> 22222
    i.e. j is left unmodified.

    Which is correct?
    Bye, Jojo
     
    Joachim Schmitz, Apr 19, 2007
    #1
    1. Advertising

  2. Joachim Schmitz

    Guest

    On 19 Apr, 07:58, "Joachim Schmitz" <> wrote:
    > Hi folks
    >
    > What would be the expected and correct output of the following
    >
    > #include <stdio.h>
    >
    > int main (void)
    > {
    > char buffer1[] = "(10,20,30)";
    > char buffer2[] = "(10,20,30 )"; /* add two white spaces between 30
    > and ) */
    > int var1, var2, var3;
    > int i = 11111;
    > int j = 22222;
    > sscanf (buffer1,"(%d,%d,%d)%n",&var1,&var2,&var3,&i);
    > printf ("%s -> %d\n",buffer1,i);
    >
    > sscanf (buffer2,"(%d,%d,%d)%n",&var1,&var2,&var3,&j);
    > printf ("%s -> %d\n",buffer2,j);
    >
    > return 0;
    >
    > }
    >
    > On one platform I get:
    > (10,20,30) -> 10
    > (10,20,30 ) -> 12
    >
    > on another I get:
    > (10,20,30) -> 10
    > (10,20,30 ) -> 22222
    > i.e. j is left unmodified.
    >
    > Which is correct?


    sscanf returns an integer which is the count of values assigned. Your
    program would be improved by showing this information, in my opinion.

    On my gcc/glibc system I get your second result and in both cases 3
    values are reported as assigned. The same result is obtained on AIX,
    using the IBM xlc compiler and IBM's C library implementation. This is
    the result I'd expect.
     
    , Apr 19, 2007
    #2
    1. Advertising

  3. <> schrieb im Newsbeitrag
    news:...
    > On 19 Apr, 07:58, "Joachim Schmitz" <> wrote:
    >> Hi folks
    >>
    >> What would be the expected and correct output of the following
    >>
    >> #include <stdio.h>
    >>
    >> int main (void)
    >> {
    >> char buffer1[] = "(10,20,30)";
    >> char buffer2[] = "(10,20,30 )"; /* add two white spaces between 30
    >> and ) */
    >> int var1, var2, var3;
    >> int i = 11111;
    >> int j = 22222;
    >> sscanf (buffer1,"(%d,%d,%d)%n",&var1,&var2,&var3,&i);
    >> printf ("%s -> %d\n",buffer1,i);
    >>
    >> sscanf (buffer2,"(%d,%d,%d)%n",&var1,&var2,&var3,&j);
    >> printf ("%s -> %d\n",buffer2,j);
    >>
    >> return 0;
    >>
    >> }
    >>
    >> On one platform I get:
    >> (10,20,30) -> 10
    >> (10,20,30 ) -> 12
    >>
    >> on another I get:
    >> (10,20,30) -> 10
    >> (10,20,30 ) -> 22222
    >> i.e. j is left unmodified.
    >>
    >> Which is correct?

    >
    > sscanf returns an integer which is the count of values assigned. Your
    > program would be improved by showing this information, in my opinion.
    >
    > On my gcc/glibc system I get your second result and in both cases 3
    > values are reported as assigned. The same result is obtained on AIX,
    > using the IBM xlc compiler and IBM's C library implementation. This is
    > the result I'd expect.

    I did try that too and sscanf returns 3 on all cases. What I'm concerned
    about thoug is the %n conversion that does not seem to work properly. So
    sscanf did process at least 9 of it's input characters "(10,20,30" as the
    corresponding variables are filled, but regardless %n doesn't show that.
    From the sscanf man-page:

    n Consumes no input. The corresponding pointer
    parameter is a pointer to an integer into
    which is written the number of characters
    read from the input string so far by this
    function. The assignment count returned at
    the completion of this function is not
    incremented.


    Bye, Jojo
     
    Joachim Schmitz, Apr 19, 2007
    #3
  4. On Apr 19, 11:58 am, "Joachim Schmitz" <> wrote:
    > Hi folks
    >
    > What would be the expected and correct output of the following
    >
    > #include <stdio.h>
    >
    > int main (void)
    > {
    > char buffer1[] = "(10,20,30)";
    > char buffer2[] = "(10,20,30 )"; /* add two white spaces between 30
    > and ) */
    > int var1, var2, var3;
    > int i = 11111;
    > int j = 22222;
    > sscanf (buffer1,"(%d,%d,%d)%n",&var1,&var2,&var3,&i);
    > printf ("%s -> %d\n",buffer1,i);
    >
    > sscanf (buffer2,"(%d,%d,%d)%n",&var1,&var2,&var3,&j);
    > printf ("%s -> %d\n",buffer2,j);
    >
    > return 0;
    >
    > }
    >
    > On one platform I get:
    > (10,20,30) -> 10
    > (10,20,30 ) -> 12
    >
    > on another I get:
    > (10,20,30) -> 10
    > (10,20,30 ) -> 22222
    > i.e. j is left unmodified.
    >
    > Which is correct?
    > Bye, Jojo


    I think second result is expected cause %n should yield the number of
    characters consumed THUS far from the input and in second case input
    consumption will end at ")" only and %n is coming later to that hence
    it's undefined.
    The C standard says: Execution of a %n directive does not increment
    the assignment count returned at the completion of execution but the
    Corrigendum seems to contradict this. Probably it is wise not to make
    any assumptions on the effect of %n conversions on the return value.

    -Cheers,
    Gunvant
    ~~~~~~~~
    No trees were killed in the sending of this message. However a large
    number of electrons were terribly inconvenienced.
     
    Gunvant Patil, Apr 19, 2007
    #4
  5. "Gunvant Patil" <> schrieb im Newsbeitrag
    news:...
    > On Apr 19, 11:58 am, "Joachim Schmitz" <> wrote:
    >> Hi folks
    >>
    >> What would be the expected and correct output of the following
    >>
    >> #include <stdio.h>
    >>
    >> int main (void)
    >> {
    >> char buffer1[] = "(10,20,30)";
    >> char buffer2[] = "(10,20,30 )"; /* add two white spaces between 30
    >> and ) */
    >> int var1, var2, var3;
    >> int i = 11111;
    >> int j = 22222;
    >> sscanf (buffer1,"(%d,%d,%d)%n",&var1,&var2,&var3,&i);
    >> printf ("%s -> %d\n",buffer1,i);
    >>
    >> sscanf (buffer2,"(%d,%d,%d)%n",&var1,&var2,&var3,&j);
    >> printf ("%s -> %d\n",buffer2,j);
    >>
    >> return 0;
    >>
    >> }
    >>
    >> On one platform I get:
    >> (10,20,30) -> 10
    >> (10,20,30 ) -> 12
    >>
    >> on another I get:
    >> (10,20,30) -> 10
    >> (10,20,30 ) -> 22222
    >> i.e. j is left unmodified.
    >>
    >> Which is correct?
    >> Bye, Jojo

    >
    > I think second result is expected cause %n should yield the number of
    > characters consumed THUS far from the input and in second case input
    > consumption will end at ")" only and %n is coming later to that hence
    > it's undefined.

    I don't understand you here, in both cases the input ends at the ")" and %n
    is immediatelly after that.

    > The C standard says: Execution of a %n directive does not increment
    > the assignment count returned at the completion of execution but the
    > Corrigendum seems to contradict this. Probably it is wise not to make
    > any assumptions on the effect of %n conversions on the return value.

    Í've seen these 4 line in the Linux man-page, but only there. What does the
    standard actually say? Chapter and verse?

    Bye, Jojo
     
    Joachim Schmitz, Apr 19, 2007
    #5
  6. Joachim Schmitz

    Guest

    On 19 Apr, 12:25, "Joachim Schmitz" <> wrote:
    > "Gunvant Patil" <> schrieb im Newsbeitragnews:...
    >
    > > On Apr 19, 11:58 am, "Joachim Schmitz" <> wrote:
    > >> Hi folks

    >
    > >> What would be the expected and correct output of the following

    >
    > >> #include <stdio.h>

    >
    > >> int main (void)
    > >> {
    > >> char buffer1[] = "(10,20,30)";
    > >> char buffer2[] = "(10,20,30 )"; /* add two white spaces between 30
    > >> and ) */
    > >> int var1, var2, var3;
    > >> int i = 11111;
    > >> int j = 22222;
    > >> sscanf (buffer1,"(%d,%d,%d)%n",&var1,&var2,&var3,&i);
    > >> printf ("%s -> %d\n",buffer1,i);

    >
    > >> sscanf (buffer2,"(%d,%d,%d)%n",&var1,&var2,&var3,&j);
    > >> printf ("%s -> %d\n",buffer2,j);

    >
    > >> return 0;

    >
    > >> }

    >
    > >> On one platform I get:
    > >> (10,20,30) -> 10
    > >> (10,20,30 ) -> 12

    >
    > >> on another I get:
    > >> (10,20,30) -> 10
    > >> (10,20,30 ) -> 22222
    > >> i.e. j is left unmodified.

    >
    > >> Which is correct?
    > >> Bye, Jojo

    >
    > > I think second result is expected cause %n should yield the number of
    > > characters consumed THUS far from the input and in second case input
    > > consumption will end at ")" only and %n is coming later to that hence
    > > it's undefined.

    >
    > I don't understand you here, in both cases the input ends at the ")" and %n
    > is immediatelly after that.


    That's not actually the case.

    In the second case "(10,20,30 )", the scanning fails at the space
    character.

    If sscanf gives up at that point (it found a character which was
    neither part of an integer nor a right parenthesis), it may well not
    even consider the "%n".
     
    , Apr 19, 2007
    #6
  7. Joachim Schmitz

    Eric Sosman Guest

    Joachim Schmitz wrote:
    > <> schrieb im Newsbeitrag
    > news:...
    >> On 19 Apr, 07:58, "Joachim Schmitz" <> wrote:
    >>> Hi folks
    >>>
    >>> What would be the expected and correct output of the following
    >>>
    >>> #include <stdio.h>
    >>>
    >>> int main (void)
    >>> {
    >>> char buffer1[] = "(10,20,30)";
    >>> char buffer2[] = "(10,20,30 )"; /* add two white spaces between 30
    >>> and ) */
    >>> int var1, var2, var3;
    >>> int i = 11111;
    >>> int j = 22222;
    >>> sscanf (buffer1,"(%d,%d,%d)%n",&var1,&var2,&var3,&i);
    >>> printf ("%s -> %d\n",buffer1,i);
    >>>
    >>> sscanf (buffer2,"(%d,%d,%d)%n",&var1,&var2,&var3,&j);
    >>> printf ("%s -> %d\n",buffer2,j);
    >>>
    >>> return 0;
    >>>
    >>> }
    >>>
    >>> On one platform I get:
    >>> (10,20,30) -> 10
    >>> (10,20,30 ) -> 12
    >>>
    >>> on another I get:
    >>> (10,20,30) -> 10
    >>> (10,20,30 ) -> 22222
    >>> i.e. j is left unmodified.
    >>>
    >>> Which is correct?

    >> sscanf returns an integer which is the count of values assigned. Your
    >> program would be improved by showing this information, in my opinion.
    >>
    >> On my gcc/glibc system I get your second result and in both cases 3
    >> values are reported as assigned. The same result is obtained on AIX,
    >> using the IBM xlc compiler and IBM's C library implementation. This is
    >> the result I'd expect.

    > I did try that too and sscanf returns 3 on all cases. What I'm concerned
    > about thoug is the %n conversion that does not seem to work properly. So
    > sscanf did process at least 9 of it's input characters "(10,20,30" as the
    > corresponding variables are filled, but regardless %n doesn't show that.
    > [...]


    Because the %n is never processed. The ')' in the format
    string does not match the space character after the "30", so
    conversion stops. Three integers have been converted and
    assigned successfully, so sscanf() returns the value 3; the
    matching failure on the ')' does not affect what has already
    happened.

    --
    Eric Sosman
    lid
     
    Eric Sosman, Apr 19, 2007
    #7
  8. On Apr 19, 4:25 pm, "Joachim Schmitz" <> wrote:
    > "Gunvant Patil" <> schrieb im Newsbeitragnews:...
    >
    >
    >
    > > On Apr 19, 11:58 am, "Joachim Schmitz" <> wrote:
    > >> Hi folks

    >
    > >> What would be the expected and correct output of the following

    >
    > >> #include <stdio.h>

    >
    > >> int main (void)
    > >> {
    > >> char buffer1[] = "(10,20,30)";
    > >> char buffer2[] = "(10,20,30 )"; /* add two white spaces between 30
    > >> and ) */
    > >> int var1, var2, var3;
    > >> int i = 11111;
    > >> int j = 22222;
    > >> sscanf (buffer1,"(%d,%d,%d)%n",&var1,&var2,&var3,&i);
    > >> printf ("%s -> %d\n",buffer1,i);

    >
    > >> sscanf (buffer2,"(%d,%d,%d)%n",&var1,&var2,&var3,&j);
    > >> printf ("%s -> %d\n",buffer2,j);

    >
    > >> return 0;

    >
    > >> }

    >
    > >> On one platform I get:
    > >> (10,20,30) -> 10
    > >> (10,20,30 ) -> 12

    >
    > >> on another I get:
    > >> (10,20,30) -> 10
    > >> (10,20,30 ) -> 22222
    > >> i.e. j is left unmodified.

    >
    > >> Which is correct?
    > >> Bye, Jojo

    >
    > > I think second result is expected cause %n should yield the number of
    > > characters consumed THUS far from the input and in second case input
    > > consumption will end at ")" only and %n is coming later to that hence
    > > it's undefined.

    >
    > I don't understand you here, in both cases the input ends at the ")" and %n
    > is immediatelly after that.


    In second case input scanning will fail at white space and not at ')'

    > > The C standard says: Execution of a %n directive does not increment
    > > the assignment count returned at the completion of execution but the
    > > Corrigendum seems to contradict this. Probably it is wise not to make
    > > any assumptions on the effect of %n conversions on the return value.

    >
    > Í've seen these 4 line in the Linux man-page, but only there. What does the
    > standard actually say? Chapter and verse?


    Yes indeed those lines are from linux man pages and here are some
    references from C99

    7.24.2.2#12

    7.24.2.2#16

    7.19.6.2 #10

    > Bye, Jojo- Hide quoted text -
    >
    > - Show quoted text -


    -Cheers,
    Gunvant
    ~~~~~~~~
    No trees were killed in the sending of this message. However a large
    number of electrons were terribly inconvenienced.
     
    Gunvant Patil, Apr 20, 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. Markus Franz

    Processes with strange behavoir

    Markus Franz, Apr 4, 2004, in forum: Python
    Replies:
    2
    Views:
    353
    Peter Otten
    Apr 4, 2004
  2. Replies:
    5
    Views:
    153
    YANAGAWA Kazuhisa
    Mar 8, 2006
  3. Sy Ys

    BackgrounDRb behavoir

    Sy Ys, May 27, 2007, in forum: Ruby
    Replies:
    1
    Views:
    115
    Ezra Zygmuntowicz
    May 27, 2007
  4. surf

    perl polymorphic behavoir ?

    surf, Feb 7, 2006, in forum: Perl Misc
    Replies:
    3
    Views:
    113
    A. Sinan Unur
    Feb 7, 2006
  5. Daniel Rucareanu

    Weird behavoir while using function aliases

    Daniel Rucareanu, Sep 19, 2006, in forum: Javascript
    Replies:
    22
    Views:
    224
    Richard Cornford
    Sep 25, 2006
Loading...

Share This Page