sscanf return value with "%n" directive

K

Kevin Goodsell

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
 
K

Kevin Goodsell

Kevin said:
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
 
R

Russell Hanneken

Kevin said:
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.).
 
C

CBFalconer

Kevin said:
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 */
 
K

Kevin Goodsell

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




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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top