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


Groups > comp.lang.c > #386392

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

From Ben Bacarisse <ben@bsb.me.uk>
Newsgroups comp.lang.c
Subject Re: The difference between strtol() and strtoul() ?
Date 2024-06-23 16:30 +0100
Organization A noiseless patient Spider
Message-ID <87jzifpth6.fsf@bsb.me.uk> (permalink)
References (3 earlier) <20240621185314.00004fda@yahoo.com> <87o77uqktg.fsf@bsb.me.uk> <20240623121952.00005fa9@yahoo.com> <87r0cnq46s.fsf@bsb.me.uk> <20240623153219.000009b0@yahoo.com>

Show all headers | View raw


Michael S <already5chosen@yahoo.com> writes:

> On Sun, 23 Jun 2024 12:38:51 +0100
> Ben Bacarisse <ben@bsb.me.uk> wrote:
>
>> Michael S <already5chosen@yahoo.com> writes:
>> 
>> > On Fri, 21 Jun 2024 18:15:07 +0100
>> > Ben Bacarisse <ben@bsb.me.uk> wrote:
>> >  
>> >> Michael S <already5chosen@yahoo.com> writes:
>> >>   
>> >> > On Fri, 21 Jun 2024 18:28:39 +0300
>> >> > Michael S <already5chosen@yahoo.com> wrote:
>> >> >    
>> >> >> 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.
>> >> >>     
>> >> >> > A result of any smaller type can be obtained by calling one of
>> >> >> > these functions and storing the result in an object of the
>> >> >> > smaller type.   
>> >> >> 
>> >> >> Or check for range and handle out of range values as
>> >> >> appropriate by situation.    
>> >> >
>> >> > BTW, I don't know what The Standard says about out-of-range
>> >> > inputs, but at least
>> >> > https://en.cppreference.com/w/c/string/byte/strtol does not say
>> >> > anything certain. especially about what stored in *str_end.    
>> >> 
>> >> It says what value should be returned.  That's something certain!
>> >>  
>> >
>> > In case of strtol, yes. 
>> > In case of strtoul it also says what value should be returned, but
>> > plain reading of cppreference.com text (at least *my* plain reading)
>> > does not match observed behaviour. The text on cppreference.com
>> > resembles Standard text, but does not match it.  
>> 
>> Ah.  What's the discrepancy you see?
>
> IMHO, the Standard texts allows for more interpretations (and
> misinterpretations) than cppreference.com text

I was hoping for an example.  As I've used these functions for decades,
I find it hard to see where the alternative interpretations might lie.

>> > Also, at least to me, Standard text itself appear very far from
>> > clear and way too open to interpretations.
>> > My own interpretation would be that for any negative input strtoul()
>> > should return ULONG_MAX and set errno to ERANGE. None of the actual
>> > implementation that I tested behaves in this manner.  
>> 
>> I don't get that from the text.  There is, after all, no "negative
>> input".  There is a "subject sequence" which, if it starts with a
>> minus sign, causes the "value resulting from the conversion is
>> negated (in the return type)" which seems clear enough.
>
> I find it less than clear.
> The most non-clear part is that for strtouxx() as long as "subject
> sequence" is in range,

I think it helps to be precise here: the subject sequence has to be of
the right form, not in the right range.

> it is first converted and then negated.  However
> when  "subject sequence" is out of range it is converted, then clipped
> and then *not* negated.

If the conversion (before negation) is out of range the result will be
ULONG_MAX and errno will be set to ERANGE.  Calling this "clipping" is
possibly confusing.  For what it's worth, I'm just describing what
happens.  I am not saying it is crystal clear.

I think there /is/ something problematic with the wording about the
negation.  It happens "in the return type" but how can
9223372036854775808 be negated in the type long long int?  OK, the
negated value can be /represented/ in the type long long int but that's
not quite the same thing.  On the othee hand, for the unsigned return
types, the negation "in the return type" is what produces ULONG_MAX for
"-1" when the negated value, -1, can't be /represented/ in the return
type.  It's a case where, over the years, I've just got used to what's
happening.

-- 
Ben.

Back to comp.lang.c | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

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

csiph-web