Re: The difference between strtol() and strtoul() ?

Liste des GroupesRevenir à cl c  
Sujet : Re: The difference between strtol() and strtoul() ?
De : 643-408-1753 (at) *nospam* kylheku.com (Kaz Kylheku)
Groupes : comp.lang.c
Date : 23. Jun 2024, 00:07:58
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <20240622145831.245@kylheku.com>
References : 1 2
User-Agent : slrn/pre1.0.4-9 (Linux)
On 2024-06-21, Kenny McCormack <gazelle@shell.xmission.com> wrote:
In article <v51d1l$2fklr$1@news.xmission.com>,
Kenny McCormack <gazelle@shell.xmission.com> wrote:
Interestingly, I note that strtoul() accepts strings that begin with a sign
(+ or -).  This is odd, since you'd (*) think that a sign (particularly, a
minus) would be a syntax error in parsing for an unsigned value.
>
There have been some useful responses on this thread, which is Good.  Of
course, there have also been the usual crappola-type responses, but one must
learn to take the good with the bad.
>
Anyway, I think the takeaway is that while it is what it is, an argument
can certainly be made that it would have been better for the unsigned
versions of these function to not accept signed input.  If I were designing
it, I would have had strtoul("-1") be a syntax error (not a C language
syntax error - but a meta-language syntax error) - or, if not that, then
have it return 1, not 2**N-1.  But that's just me.

An alternative would be for the current minus handling behavior to apply
when the base is specified as zero, which is where the other hacks are
like leading 0 for octal and 0x  for hexadecimal (that one also
recognized in base 16).

I appreciate the responses indicating that it was probably done the way it
was for actually both of these reasons:
    1) Because it makes it more useful for C compiler writers - who were
seen as the primary audience.
    2) Because it means that the two functions are literally the same code.
Both calculate the same bit pattern - the difference is only in the
caller's interpretation of the result.

3) The behavior is also useful for IT people who understand two's
complement computer arithmetic:

  voipserver --debug-mask=-1  # more convenient than --debug-mask=0xFFFFFFFF

It's why the 0x prefix is supported when base is 0, and also octal.

It supports not only compiler writing but system utilities.

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca

Date Sujet#  Auteur
20 Jun 24 * The difference between strtol() and strtoul() ?45Kenny McCormack
20 Jun 24 +- Re: The difference between strtol() and strtoul() ?1Lew Pitcher
20 Jun 24 +- Re: The difference between strtol() and strtoul() ?1Lew Pitcher
20 Jun 24 +- Re: The difference between strtol() and strtoul() ?1Keith Thompson
21 Jun 24 +* Re: The difference between strtol() and strtoul() ?2Kaz Kylheku
21 Jun 24 i`- Re: The difference between strtol() and strtoul() ?1Kenny McCormack
21 Jun 24 +* Re: The difference between strtol() and strtoul() ?38Kenny McCormack
21 Jun 24 i+* Re: The difference between strtol() and strtoul() ?34Michael S
21 Jun 24 ii+* Re: The difference between strtol() and strtoul() ?27Michael S
21 Jun 24 iii+* Re: The difference between strtol() and strtoul() ?19Ben Bacarisse
23 Jun 24 iiii`* Re: The difference between strtol() and strtoul() ?18Michael S
23 Jun 24 iiii `* Re: The difference between strtol() and strtoul() ?17Ben Bacarisse
23 Jun 24 iiii  `* Re: The difference between strtol() and strtoul() ?16Michael S
23 Jun 24 iiii   `* Re: The difference between strtol() and strtoul() ?15Ben Bacarisse
23 Jun 24 iiii    +- Re: The difference between strtol() and strtoul() ?1Michael S
23 Jun 24 iiii    +* Re: The difference between strtol() and strtoul() ?12Tim Rentsch
24 Jun 24 iiii    i+* Re: The difference between strtol() and strtoul() ?10Keith Thompson
24 Jun 24 iiii    ii+* Re: The difference between strtol() and strtoul() ?8Ben Bacarisse
24 Jun 24 iiii    iii+- Re: The difference between strtol() and strtoul() ?1Keith Thompson
24 Jun 24 iiii    iii`* Re: The difference between strtol() and strtoul() ?6Kaz Kylheku
24 Jun 24 iiii    iii +* Re: The difference between strtol() and strtoul() ?3Kaz Kylheku
24 Jun 24 iiii    iii i`* Re: The difference between strtol() and strtoul() ?2Keith Thompson
24 Jun 24 iiii    iii i `- Re: The difference between strtol() and strtoul() ?1Kaz Kylheku
24 Jun 24 iiii    iii `* Re: The difference between strtol() and strtoul() ?2Keith Thompson
24 Jun 24 iiii    iii  `- Re: The difference between strtol() and strtoul() ?1Michael S
24 Jun 24 iiii    ii`- Re: The difference between strtol() and strtoul() ?1Tim Rentsch
24 Jun 24 iiii    i`- Re: The difference between strtol() and strtoul() ?1Tim Rentsch
24 Jun 24 iiii    `- Re: The difference between strtol() and strtoul() ?1Lawrence D'Oliveiro
21 Jun 24 iii+* Re: The difference between strtol() and strtoul() ?4James Kuyper
21 Jun 24 iiii+* Re: The difference between strtol() and strtoul() ?2Kenny McCormack
23 Jun 24 iiiii`- Re: The difference between strtol() and strtoul() ?1Michael S
22 Jun 24 iiii`- Re: The difference between strtol() and strtoul() ?1Michael S
22 Jun 24 iii`* Re: The difference between strtol() and strtoul() ?3Lawrence D'Oliveiro
23 Jun 24 iii +- Re: The difference between strtol() and strtoul() ?1Lawrence D'Oliveiro
23 Jun 24 iii `- Re: The difference between strtol() and strtoul() ?1James Kuyper
21 Jun 24 ii`* Re: The difference between strtol() and strtoul() ?6Ben Bacarisse
21 Jun 24 ii `* Re: The difference between strtol() and strtoul() ?5Keith Thompson
22 Jun 24 ii  +- Re: The difference between strtol() and strtoul() ?1Lawrence D'Oliveiro
22 Jun 24 ii  `* Re: The difference between strtol() and strtoul() ?3Michael S
23 Jun 24 ii   +- Re: The difference between strtol() and strtoul() ?1Lawrence D'Oliveiro
23 Jun 24 ii   `- Re: The difference between strtol() and strtoul() ?1Keith Thompson
21 Jun 24 i+* Re: The difference between strtol() and strtoul() ?2Michael S
21 Jun 24 ii`- Re: The difference between strtol() and strtoul() ?1Keith Thompson
23 Jun 24 i`- Re: The difference between strtol() and strtoul() ?1Kaz Kylheku
23 Jun 24 `- Re: The difference between strtol() and strtoul() ?1Richard Kettlewell

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal