delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp-workers/2000/09/29/08:41:18

X-Authentication-Warning: acp3bf.physik.rwth-aachen.de: broeker owned process doing -bs
Date: Fri, 29 Sep 2000 14:39:48 +0200 (MET DST)
From: Hans-Bernhard Broeker <broeker AT physik DOT rwth-aachen DOT de>
X-Sender: broeker AT acp3bf
To: djgpp-workers AT delorie DOT com
Subject: Re: scanf and invalid FP fields
In-Reply-To: <2561-Fri29Sep2000142530+0300-eliz@is.elta.co.il>
Message-ID: <Pine.LNX.4.10.10009291403580.29790-100000@acp3bf>
MIME-Version: 1.0
Reply-To: djgpp-workers AT delorie DOT com
Errors-To: nobody AT delorie DOT com
X-Mailing-List: djgpp-workers AT delorie DOT com
X-Unsubscribes-To: listserv AT delorie DOT com

On Fri, 29 Sep 2000, Eli Zaretskii wrote:

> Consider the program below.  It is a variant of one of the examples in
> the Draft C9X Standard (example 3 on page 271).

Note this very same example is also present in the old C89 standard (as
reprinted in P.J.Plauger 'The Standard C Library', page 243). OTOH, the
examples shown in the Standard body itself are not normative, i.e. we
don't have to strictly imitate every example's behaviour.

So this is not a change in C99 or something, it's been like that since the
beginning of time_t (well, roughly :-).

>      100ergs of energy
> 
> Since the format is "%f%20s of %20s", I think this should store 100 in
> the float variable that corresponds to the %f conversion specifiers,
> and leave the following `e' for the char [] variable which is
> converted by %20s.

> Our current implementation of _doscan indeed stores 100 in the FP
> variable, but swallows `e', and stores only "rgs" in the char []
> variable.  I think this is wrong, since "100e" is not a valid FP
> number.

This certainly is wrong behaviour, yes. No matter what the opinion on the
matching failure issue is, we cannot just drop that 'e' into nothing-ness
after we did claim a successful match of the "100".

I used a slightly different test program, to ease testing a bit:

#include <stdio.h>

int main(void)
{
    int count, charsread;
    float quant = -1;
    char s1[21], s2[21];
    
    char input[100] = "100ergs of energy .....";
    
    count = sscanf(input, "%f%20s of %20s%n", &quant, s1, s2, &charsread);
    printf ("count: %d quant: %f units: %s item: %s\n",
            count, quant, s1, s2);
    printf ("remainder: \"%s\"\n", input + charsread);
    
    return 0;
}

This generated almost exactly the same output on all Unix platforms I
tried (Solaris, DEC Unix, IRIX, Linux-glibc5):

count: 3 quant: 100.000000 units: ergs item: energy
remainder: "....."

Some had a slightly different 'remainder':

count: 3 quant: 100.000000 units: ergs item: energy
remainder: " ....."

(note the leading blank).

So it seems like the example is, indeed, in contrast to existing practice
on Unix libc's. A query in comp.lang.c.std might clarify the status of
that particular example.

Hans-Bernhard Broeker (broeker AT physik DOT rwth-aachen DOT de)
Even if all the snow were burnt, ashes would remain.

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019