By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,771 Members | 1,685 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,771 IT Pros & Developers. It's quick & easy.

sscanf return value with "%n" directive

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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.