Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #386268 > unrolled thread

The difference between strtol() and strtoul() ?

Started bygazelle@shell.xmission.com (Kenny McCormack)
First post2024-06-20 14:06 +0000
Last post2024-06-23 17:39 +0100
Articles 10 on this page of 50 — 11 participants

Back to article view | Back to comp.lang.c


Contents

  The difference between strtol() and strtoul() ? gazelle@shell.xmission.com (Kenny McCormack) - 2024-06-20 14:06 +0000
    Re: The difference between strtol() and strtoul() ? scott@slp53.sl.home (Scott Lurndal) - 2024-06-20 14:46 +0000
      Re: The difference between strtol() and strtoul() ? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-06-20 14:37 -0700
    Re: The difference between strtol() and strtoul() ? Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2024-06-20 14:48 +0000
    Re: The difference between strtol() and strtoul() ? Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2024-06-20 15:26 +0000
    Re: The difference between strtol() and strtoul() ? Kaz Kylheku <643-408-1753@kylheku.com> - 2024-06-20 22:55 +0000
      Re: The difference between strtol() and strtoul() ? gazelle@shell.xmission.com (Kenny McCormack) - 2024-06-20 23:35 +0000
    Re: The difference between strtol() and strtoul() ? gazelle@shell.xmission.com (Kenny McCormack) - 2024-06-21 13:58 +0000
      Re: The difference between strtol() and strtoul() ? Michael S <already5chosen@yahoo.com> - 2024-06-21 18:28 +0300
        Re: The difference between strtol() and strtoul() ? Michael S <already5chosen@yahoo.com> - 2024-06-21 18:53 +0300
          Re: The difference between strtol() and strtoul() ? scott@slp53.sl.home (Scott Lurndal) - 2024-06-21 16:14 +0000
            Re: The difference between strtol() and strtoul() ? scott@slp53.sl.home (Scott Lurndal) - 2024-06-21 16:54 +0000
              Re: The difference between strtol() and strtoul() ? Lawrence D'Oliveiro <ldo@nz.invalid> - 2024-06-22 06:44 +0000
                Re: The difference between strtol() and strtoul() ? scott@slp53.sl.home (Scott Lurndal) - 2024-06-22 15:16 +0000
                  Re: The difference between strtol() and strtoul() ? Lawrence D'Oliveiro <ldo@nz.invalid> - 2024-06-22 23:21 +0000
                  Re: The difference between strtol() and strtoul() ? James Kuyper <jameskuyper@alumni.caltech.edu> - 2024-06-22 20:10 -0400
          Re: The difference between strtol() and strtoul() ? Ben Bacarisse <ben@bsb.me.uk> - 2024-06-21 18:15 +0100
            Re: The difference between strtol() and strtoul() ? Michael S <already5chosen@yahoo.com> - 2024-06-23 12:19 +0300
              Re: The difference between strtol() and strtoul() ? Ben Bacarisse <ben@bsb.me.uk> - 2024-06-23 12:38 +0100
                Re: The difference between strtol() and strtoul() ? Michael S <already5chosen@yahoo.com> - 2024-06-23 15:32 +0300
                  Re: The difference between strtol() and strtoul() ? Ben Bacarisse <ben@bsb.me.uk> - 2024-06-23 16:30 +0100
                    Re: The difference between strtol() and strtoul() ? Michael S <already5chosen@yahoo.com> - 2024-06-23 18:47 +0300
                    Re: The difference between strtol() and strtoul() ? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2024-06-23 10:58 -0700
                      Re: The difference between strtol() and strtoul() ? scott@slp53.sl.home (Scott Lurndal) - 2024-06-23 21:19 +0000
                        Re: The difference between strtol() and strtoul() ? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2024-06-23 22:28 -0700
                      Re: The difference between strtol() and strtoul() ? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-06-23 16:01 -0700
                        Re: The difference between strtol() and strtoul() ? Ben Bacarisse <ben@bsb.me.uk> - 2024-06-24 00:49 +0100
                          Re: The difference between strtol() and strtoul() ? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-06-23 17:49 -0700
                          Re: The difference between strtol() and strtoul() ? Kaz Kylheku <643-408-1753@kylheku.com> - 2024-06-24 02:29 +0000
                            Re: The difference between strtol() and strtoul() ? Kaz Kylheku <643-408-1753@kylheku.com> - 2024-06-24 02:31 +0000
                              Re: The difference between strtol() and strtoul() ? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-06-23 20:12 -0700
                                Re: The difference between strtol() and strtoul() ? Kaz Kylheku <643-408-1753@kylheku.com> - 2024-06-24 06:05 +0000
                            Re: The difference between strtol() and strtoul() ? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-06-23 20:11 -0700
                              Re: The difference between strtol() and strtoul() ? Michael S <already5chosen@yahoo.com> - 2024-06-24 13:19 +0300
                        Re: The difference between strtol() and strtoul() ? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2024-06-23 22:30 -0700
                    Re: The difference between strtol() and strtoul() ? Lawrence D'Oliveiro <ldo@nz.invalid> - 2024-06-24 00:48 +0000
          Re: The difference between strtol() and strtoul() ? James Kuyper <jameskuyper@alumni.caltech.edu> - 2024-06-21 14:38 -0400
            Re: The difference between strtol() and strtoul() ? gazelle@shell.xmission.com (Kenny McCormack) - 2024-06-21 18:43 +0000
              Re: The difference between strtol() and strtoul() ? Michael S <already5chosen@yahoo.com> - 2024-06-23 11:47 +0300
            Re: The difference between strtol() and strtoul() ? Michael S <already5chosen@yahoo.com> - 2024-06-22 21:18 +0300
        Re: The difference between strtol() and strtoul() ? Ben Bacarisse <ben@bsb.me.uk> - 2024-06-21 18:02 +0100
          Re: The difference between strtol() and strtoul() ? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-06-21 10:38 -0700
            Re: The difference between strtol() and strtoul() ? Lawrence D'Oliveiro <ldo@nz.invalid> - 2024-06-22 06:43 +0000
            Re: The difference between strtol() and strtoul() ? Michael S <already5chosen@yahoo.com> - 2024-06-22 21:04 +0300
              Re: The difference between strtol() and strtoul() ? Lawrence D'Oliveiro <ldo@nz.invalid> - 2024-06-22 23:22 +0000
              Re: The difference between strtol() and strtoul() ? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-06-22 16:43 -0700
      Re: The difference between strtol() and strtoul() ? Michael S <already5chosen@yahoo.com> - 2024-06-21 19:00 +0300
        Re: The difference between strtol() and strtoul() ? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-06-21 10:50 -0700
      Re: The difference between strtol() and strtoul() ? Kaz Kylheku <643-408-1753@kylheku.com> - 2024-06-22 22:07 +0000
    Re: The difference between strtol() and strtoul() ? Richard Kettlewell <invalid@invalid.invalid> - 2024-06-23 17:39 +0100

Page 3 of 3 — ← Prev page 1 2 [3]


#386323

FromBen Bacarisse <ben@bsb.me.uk>
Date2024-06-21 18:02 +0100
Message-ID<87tthmqlej.fsf@bsb.me.uk>
In reply to#386318
Michael S <already5chosen@yahoo.com> writes:

> On Fri, 21 Jun 2024 13:58:01 -0000 (UTC)
> gazelle@shell.xmission.com (Kenny McCormack) wrote:
>> 
>> Yeah, now I get it.  You really only need strtoimax() and strtoumax().
>
> Which are? uunfortunately, not part of C standard.

Not sure if that '?' is just a typo.  Anyway, yes they are both part of
the C standard.

-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#386326

FromKeith Thompson <Keith.S.Thompson+u@gmail.com>
Date2024-06-21 10:38 -0700
Message-ID<874j9mw5zo.fsf@nosuchdomain.example.com>
In reply to#386323
Ben Bacarisse <ben@bsb.me.uk> writes:
> Michael S <already5chosen@yahoo.com> writes:
[...]
>> Which are? uunfortunately, not part of C standard.
>
> Not sure if that '?' is just a typo.  Anyway, yes they are both part of
> the C standard.

strto[u]l[l] are declared in <stdlib.h>  strtoimax and strtoumax are
declared in <inttypes.h>, which can make them easy to miss.

-- 
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
void Void(void) { Void(); } /* The recursive call of the void */

[toc] | [prev] | [next] | [standalone]


#386341

FromLawrence D'Oliveiro <ldo@nz.invalid>
Date2024-06-22 06:43 +0000
Message-ID<v55rr1$3ktut$1@dont-email.me>
In reply to#386326
On Fri, 21 Jun 2024 10:38:51 -0700, Keith Thompson wrote:

> strto[u]l[l] are declared in <stdlib.h>  strtoimax and strtoumax are
> declared in <inttypes.h>, which can make them easy to miss.

The first thing I do is check the man pages
<https://manpages.debian.org/3/strtoimax.3.en.html>:

    STANDARDS

        POSIX.1-2001, POSIX.1-2008, C99.

[toc] | [prev] | [next] | [standalone]


#386353

FromMichael S <already5chosen@yahoo.com>
Date2024-06-22 21:04 +0300
Message-ID<20240622210438.0000018f@yahoo.com>
In reply to#386326
On Fri, 21 Jun 2024 10:38:51 -0700
Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote:

> Ben Bacarisse <ben@bsb.me.uk> writes:
> > Michael S <already5chosen@yahoo.com> writes:  
> [...]
> >> Which are? uunfortunately, not part of C standard.  
> >
> > Not sure if that '?' is just a typo.  Anyway, yes they are both
> > part of the C standard.  
> 
> strto[u]l[l] are declared in <stdlib.h>  strtoimax and strtoumax are
> declared in <inttypes.h>, which can make them easy to miss.
> 

May be, that is the reason. But frankly, I expected that
cppreference.com will do better. As a minimum, strtoimax should have
ben listed in "See also" section on this page:
https://en.cppreference.com/w/c/string/byte/strtol

[toc] | [prev] | [next] | [standalone]


#386365

FromLawrence D'Oliveiro <ldo@nz.invalid>
Date2024-06-22 23:22 +0000
Message-ID<v57mbj$3vq0p$3@dont-email.me>
In reply to#386353
On Sat, 22 Jun 2024 21:04:38 +0300, Michael S wrote:

> But frankly, I expected that cppreference.com will do better.

This is why we have authoritative references.

[toc] | [prev] | [next] | [standalone]


#386368

FromKeith Thompson <Keith.S.Thompson+u@gmail.com>
Date2024-06-22 16:43 -0700
Message-ID<87h6dkv8zw.fsf@nosuchdomain.example.com>
In reply to#386353
Michael S <already5chosen@yahoo.com> writes:
> On Fri, 21 Jun 2024 10:38:51 -0700
> Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote:
>> Ben Bacarisse <ben@bsb.me.uk> writes:
>> > Michael S <already5chosen@yahoo.com> writes:  
>> [...]
>> >> Which are? uunfortunately, not part of C standard.  
>> >
>> > Not sure if that '?' is just a typo.  Anyway, yes they are both
>> > part of the C standard.  
>> 
>> strto[u]l[l] are declared in <stdlib.h>  strtoimax and strtoumax are
>> declared in <inttypes.h>, which can make them easy to miss.
>
> May be, that is the reason. But frankly, I expected that
> cppreference.com will do better. As a minimum, strtoimax should have
> ben listed in "See also" section on this page:
> https://en.cppreference.com/w/c/string/byte/strtol

I agree, and I'm going to suggest that change.  (Editing of the page is
currently disabled for new users due to vandalism and I've had some
problems with my account.)

But <https://en.cppreference.com/w/c/string/byte/strtoimax> does
indicate that both functions are declared in <inttypes.h> and has
references to the C99 and later standards.

-- 
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
void Void(void) { Void(); } /* The recursive call of the void */

[toc] | [prev] | [next] | [standalone]


#386320

FromMichael S <already5chosen@yahoo.com>
Date2024-06-21 19:00 +0300
Message-ID<20240621190008.00006c1e@yahoo.com>
In reply to#386316
On Fri, 21 Jun 2024 13:58:01 -0000 (UTC)
gazelle@shell.xmission.com (Kenny McCormack) wrote:

> In article <v51d1l$2fklr$1@news.xmission.com>,
>     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.
> 

I implementation that I just tested strtoll and strtull are not the
same. They deliver different answers when input is out of range.

[toc] | [prev] | [next] | [standalone]


#386327

FromKeith Thompson <Keith.S.Thompson+u@gmail.com>
Date2024-06-21 10:50 -0700
Message-ID<87zfreuqvj.fsf@nosuchdomain.example.com>
In reply to#386320
Michael S <already5chosen@yahoo.com> writes:
> On Fri, 21 Jun 2024 13:58:01 -0000 (UTC)
[...]
> I implementation that I just tested strtoll and strtull are not the
> same. They deliver different answers when input is out of range.

Yes, that's the required behavior.  N1570 7.22.1.4p8:
"""
The strtol, strtoll, strtoul, and strtoull functions return the
converted value, if any. If no conversion could be performed, zero is
returned. If the correct value is outside the range of representable
values, LONG_MIN, LONG_MAX, LLONG_MIN, LLONG_MAX, ULONG_MAX, or
ULLONG_MAX is returned (according to the return type and sign of the
value, if any), and the value of the macro ERANGE is stored in errno.
"""

N3220 has identical wording in 7.24.1.7p8.  The wording for strtoimax
and strtoumax (<inttypes.h>) is equivalent.

-- 
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
void Void(void) { Void(); } /* The recursive call of the void */

[toc] | [prev] | [next] | [standalone]


#386362

FromKaz Kylheku <643-408-1753@kylheku.com>
Date2024-06-22 22:07 +0000
Message-ID<20240622145831.245@kylheku.com>
In reply to#386316
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

[toc] | [prev] | [next] | [standalone]


#386394

FromRichard Kettlewell <invalid@invalid.invalid>
Date2024-06-23 17:39 +0100
Message-ID<wwvsex3aa0m.fsf@LkoBDZeT.terraraq.uk>
In reply to#386268
gazelle@shell.xmission.com (Kenny McCormack) writes
> 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.
>
> Further, although the (Linux) man page is more than a bit murky on the
> subject, it seems that the result of parsing, say, "-1", with
> strtoul() is the largest unsigned value (usually, 2**N-1 or a lot of
> F's (in hex)).  Whereas, I would expect it to be 1 (i.e., just take
> the absolute value).
>
> Comments?  I find this all very counterintuitive.

I can think of contexts where the string -1 would be read as meaning 1
(e.g. GF(2^n)) but I don’t think most people would think they were a
sensible analogy for stroul behavior. Its behavior seems consistent with
the normal meaning of unary minus (i.e. additive inverse) and of course
with C’s treatment of unsigned integer types.

-- 
https://www.greenend.org.uk/rjk/

[toc] | [prev] | [standalone]


Page 3 of 3 — ← Prev page 1 2 [3]

Back to top | Article view | comp.lang.c


csiph-web