Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 09 Jun 1997 19:55:09 +0200
From:      Arne Henrik Juul <arnej@stud.math.ntnu.no>
To:        msmith@atrad.adelaide.edu.au
Cc:        hackers@FreeBSD.ORG
Subject:   Re: %i conversion in sscanf?
Message-ID:  <199706091755.KAA04846@hub.freebsd.org>
In-Reply-To: Your message of "Tue, 10 Jun 1997 00:22:20 %2B0930 (CST)"
References:  <199706091452.AAA29091@genesis.atrad.adelaide.edu.au>

next in thread | previous in thread | raw e-mail | index | archive | help
Michael Smith <msmith@atrad.adelaide.edu.au> writes:
> To cut a long story short; from sscanf(3) :
> 
>      i     Matches an optionally signed integer; the next pointer must be a
>            pointer to int. The integer is read in base 16 if it begins with
>            `0x' or `0X', in base 8 if it begins with `0', and in base 10 oth-
>            erwise.  Only characters that correspond to the base are used.
> 
> Ok.  Fairly ambiguous.  Some test examples :
> 
> token: '0x12345678' -> int 0x12345678
> token: '0xABCDEF45' -> int 0x7fffffff
> 
> Hmm.  Not _necessarily_ expected, although it makes reasonable sense.
> However, what I wonder is is this "right" by whatever standard governs
> sscanf()?

ANSI C mandates that scanf %i should work like strtol() with base=0;
which again should work the same way that integer constants in the
code does (but with an optional preceding plus or minus sign).

Then it goes on (for strtol) to say: "If the correct value is outside
the range of representable values, LONG_MAX or LONG_MIN is returned
(according to the sign of the value), and the value of the macro
ERANGE is stored in errno."

I take a wild guess your application really wanted to use strtoul(),
which isn't available everywhere (though it's in ANSI C so it should
be common).

  -  Arne H. J.




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199706091755.KAA04846>