469,613 Members | 1,534 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,613 developers. It's quick & easy.

sscanf return value with "%n" directive

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.

Nov 13 '05 #1
4 6289
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.

Nov 13 '05 #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?
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
rg********@pobox.com
Remove the 'g' from my address to send me mail.

Nov 13 '05 #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. 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 (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!

Nov 13 '05 #4
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.

Nov 13 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by lvcha.gouqizi | last post: by
32 posts views Thread by Mike Machuidel | last post: by
10 posts views Thread by LaEisem | last post: by
5 posts views Thread by Dmitriy Lapshin [C# / .NET MVP] | last post: by
32 posts views Thread by Axel Bock | last post: by
4 posts views Thread by yaru22 | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.