Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > sscanf return value with "%n" directive

Reply
Thread Tools

sscanf return value with "%n" directive

 
 
Kevin Goodsell
Guest
Posts: n/a
 
      10-20-2003
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.

 
Reply With Quote
 
 
 
 
Kevin Goodsell
Guest
Posts: n/a
 
      10-20-2003
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.

 
Reply With Quote
 
 
 
 
Russell Hanneken
Guest
Posts: n/a
 
      10-20-2003
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
http://www.velocityreviews.com/forums/(E-Mail Removed)
Remove the 'g' from my address to send me mail.



 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      10-20-2003
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 ((E-Mail Removed)) ((E-Mail Removed))
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!

 
Reply With Quote
 
Kevin Goodsell
Guest
Posts: n/a
 
      10-20-2003
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
sscanf return value gio C Programming 7 04-22-2007 12:00 AM
How to sscanf return integer only Yogi_Bear_79 C Programming 17 05-22-2006 12:45 AM
what value does lack of return or empty "return;" return Greenhorn C Programming 15 03-06-2005 08:19 PM
The 'Location' attribute is not supported by the 'OutPutCache' directive????when i set in the directive .NET Follower ASP .Net 1 02-13-2004 10:32 AM
Return a return value from Perl to Javascript PvdK Perl 0 07-24-2003 09:20 AM



Advertisments