sscanf return value with "%n" directive

Discussion in 'C Programming' started by Kevin Goodsell, Oct 20, 2003.

  1. The return value of sscanf should be "the number of input items
    assigned" (unless an input failure occurs before any conversion). Are
    items assigned due to a "%n" directive included in the returned count?

    Thanks.

    -Kevin
    --
    My email address is valid, but changes periodically.
    To contact me please use the address from a recent posting.
    Kevin Goodsell, Oct 20, 2003
    #1
    1. Advertising

  2. Kevin Goodsell wrote:

    > The return value of sscanf should be "the number of input items
    > assigned" (unless an input failure occurs before any conversion). Are
    > items assigned due to a "%n" directive included in the returned count?
    >
    > Thanks.
    >


    Follow-up question: Is the answer to my first question the same for both
    C89 and C99? If not, what's the difference?

    Thanks again.

    -Kevin
    --
    My email address is valid, but changes periodically.
    To contact me please use the address from a recent posting.
    Kevin Goodsell, Oct 20, 2003
    #2
    1. Advertising

  3. Kevin Goodsell wrote:
    >
    > The return value of sscanf should be "the number of input items
    > assigned" (unless an input failure occurs before any conversion). Are
    > items assigned due to a "%n" directive included in the returned count?


    No (Source: K&R, page 246).

    > Follow-up question: Is the answer to my first question the same for both
    > C89 and C99?


    Yes (Source: C99, section 7.19.6.2.).

    --
    Russell Hanneken

    Remove the 'g' from my address to send me mail.
    Russell Hanneken, Oct 20, 2003
    #3
  4. Kevin Goodsell

    CBFalconer Guest

    Kevin Goodsell wrote:
    >
    > The return value of sscanf should be "the number of input items
    > assigned" (unless an input failure occurs before any conversion).
    > Are items assigned due to a "%n" directive included in the
    > returned count?


    No. From N869, 7.19.6.2:

    n No input is consumed. The corresponding argument
    shall be a pointer to signed integer into which is
    to be written the number of characters read from the
    input stream so far by this call to the fscanf
    function. Execution of a %n directive does not
    increment the assignment count returned at the
    completion of execution of the fscanf function. No
    argument is converted, but one is consumed. If the
    conversion specification includes an assignment-
    suppressing character or a field width, the behavior
    is undefined.

    and the specification for C89 is the same. This is also shown by
    the following program (ggets mallocs space for and inputs a
    complete line):

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

    int countints(void)
    {
    char *ln;
    int num;
    int count;
    int ix, delta;

    count = 0; ix = 0;
    if (0 == ggets(&ln)) {
    while (1 == sscanf(&ln[ix], "%d%n", &num, &delta)) {
    printf("[%d]%d ", ix, num);
    ix += delta;
    count++;
    }
    printf(": %d\n", count);
    free(ln);
    }
    return count;
    } /* countints */

    /* ------------------ */

    int main(void)
    {
    while (countints()) continue;
    return 0;
    } /* main */

    --
    Chuck F () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE worldnet address!
    CBFalconer, Oct 20, 2003
    #4
  5. Russell Hanneken wrote:
    > Kevin Goodsell wrote:
    >
    >>The return value of sscanf should be "the number of input items
    >>assigned" (unless an input failure occurs before any conversion). Are
    >>items assigned due to a "%n" directive included in the returned count?

    >
    >
    > No (Source: K&R, page 246).
    >
    >
    >>Follow-up question: Is the answer to my first question the same for both
    >>C89 and C99?

    >
    >
    > Yes (Source: C99, section 7.19.6.2.).
    >


    Both of those sources were right in front of me and I managed to miss
    the answer in both. For some reason I didn't think it would be in the
    description of %n, so I barely glanced at that. :-/

    Thanks Russell & Chuck.

    -Kevin
    --
    My email address is valid, but changes periodically.
    To contact me please use the address from a recent posting.
    Kevin Goodsell, Oct 20, 2003
    #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. PvdK
    Replies:
    0
    Views:
    2,978
  2. Seong-Kook Shin
    Replies:
    1
    Views:
    491
    Richard Bos
    Jun 18, 2004
  3. Greenhorn
    Replies:
    15
    Views:
    812
    Keith Thompson
    Mar 6, 2005
  4. Yogi_Bear_79

    How to sscanf return integer only

    Yogi_Bear_79, May 5, 2006, in forum: C Programming
    Replies:
    17
    Views:
    523
    Dave Thompson
    May 22, 2006
  5. gio

    sscanf return value

    gio, Apr 21, 2007, in forum: C Programming
    Replies:
    7
    Views:
    816
    Chris Torek
    Apr 22, 2007
Loading...

Share This Page