strtol library function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • David Scarlett

    strtol library function

    Another two questions...

    Is behaviour defined when the first argument of strtol is NULL?

    And if the string contains only digits, is the 2nd argument (assuming
    it wasn't NULL) guaranteed to be set to a pointer to a pointer to '\0'?


    Thanks.

    --
    David Scarlett

    dscarlett@_ _ _ _ _ _ _ _
    _ _ _ _ _ optusnet.com.au
  • P.J. Plauger

    #2
    Re: strtol library function

    "David Scarlett" <look@my.signat ure> wrote in message
    news:Xns94EAC87 161287dscarlett @211.29.133.50. ..
    [color=blue]
    > Another two questions...
    >
    > Is behaviour defined when the first argument of strtol is NULL?[/color]

    No.
    [color=blue]
    > And if the string contains only digits, is the 2nd argument (assuming
    > it wasn't NULL) guaranteed to be set to a pointer to a pointer to '\0'?[/color]

    The second argument is passed by value as a pointer to pointer. It makes
    no sense to talke about what it might get set to. But the pointer it
    points at on a successful return should designate the terminating NUL,
    in the case you describe. I think that's what you really meant.

    P.J. Plauger
    Dinkumware, Ltd.



    Comment

    • Al Bowers

      #3
      Re: strtol library function



      David Scarlett wrote:[color=blue]
      > Another two questions...
      >
      > Is behaviour defined when the first argument of strtol is NULL?
      >[/color]

      The first agrument must be a value that represents a pointer to
      a string. A value of NULL is not defined for the function.

      I have a question regarding errno. Given the following code, which
      simply detects if there was a proper coversion or not, is the test
      using LONG_MIN or LONG_MAX neccessary or can one use use the test
      errno == ERANGE?

      #include <stdio.h>
      #include <limits.h>
      #include <errno.h>
      #include <stdlib.h>

      int main(void)
      {
      char *str = "-123456789999999 999999", *s;
      long num = 0;

      errno = 0;
      num = strtol(str,&s,1 0);
      if(s == str || *s != '\0' ||
      ((errno == ERANGE) && (num == LONG_MAX || num == LONG_MIN)))
      printf("\"%s\" does not convert to a long value\n",str);
      else
      printf("\"%s\" = %ld\n",str,num) ;
      return 0;
      }


      --
      Al Bowers
      Tampa, Fl USA
      mailto: xabowers@myrapi dsys.com (remove the x to send email)
      Latest news coverage, email, free stock quotes, live scores and video are just the beginning. Discover more every day at Yahoo!


      Comment

      • P.J. Plauger

        #4
        Re: strtol library function

        "Al Bowers" <xabowers@rapid sys.com> wrote in message
        news:2gmoafF4kc 7aU1@uni-berlin.de...
        [color=blue]
        > I have a question regarding errno. Given the following code, which
        > simply detects if there was a proper coversion or not, is the test
        > using LONG_MIN or LONG_MAX neccessary or can one use use the test
        > errno == ERANGE?
        >
        > #include <stdio.h>
        > #include <limits.h>
        > #include <errno.h>
        > #include <stdlib.h>
        >
        > int main(void)
        > {
        > char *str = "-123456789999999 999999", *s;
        > long num = 0;
        >
        > errno = 0;
        > num = strtol(str,&s,1 0);
        > if(s == str || *s != '\0' ||
        > ((errno == ERANGE) && (num == LONG_MAX || num == LONG_MIN)))
        > printf("\"%s\" does not convert to a long value\n",str);
        > else
        > printf("\"%s\" = %ld\n",str,num) ;
        > return 0;
        > }[/color]

        The test of errno should suffice.

        P.J. Plauger
        Dinkumware, Ltd.



        Comment

        • Al Bowers

          #5
          Re: strtol library function



          P.J. Plauger wrote:[color=blue]
          > "Al Bowers" <xabowers@rapid sys.com> wrote in message
          > news:2gmoafF4kc 7aU1@uni-berlin.de...
          >
          >[color=green]
          >>I have a question regarding errno. Given the following code, which
          >>simply detects if there was a proper coversion or not, is the test
          >>using LONG_MIN or LONG_MAX neccessary or can one use use the test
          >>errno == ERANGE?
          >>
          >>#include <stdio.h>
          >>#include <limits.h>
          >>#include <errno.h>
          >>#include <stdlib.h>
          >>
          >>int main(void)
          >>{
          >> char *str = "-123456789999999 999999", *s;
          >> long num = 0;
          >>
          >> errno = 0;
          >> num = strtol(str,&s,1 0);
          >> if(s == str || *s != '\0' ||
          >> ((errno == ERANGE) && (num == LONG_MAX || num == LONG_MIN)))
          >> printf("\"%s\" does not convert to a long value\n",str);
          >> else
          >> printf("\"%s\" = %ld\n",str,num) ;
          >> return 0;
          >>}[/color]
          >
          >
          > The test of errno should suffice.
          >[/color]

          Yeah! That is the way I always used it. But recently someone mentioned
          that there is nothing to prevent the a function implementation from
          setting errno to ERANGE even if the resulting value is in the range
          of a long. I looked at the Standard's description of the function and
          saw that it id say the an unrepresentable value would result in both
          errno == ERANGE and the return value either LONG_MAX or LONG_MIN.

          --
          Al Bowers
          Tampa, Fl USA
          mailto: xabowers@myrapi dsys.com (remove the x to send email)
          Latest news coverage, email, free stock quotes, live scores and video are just the beginning. Discover more every day at Yahoo!


          Comment

          • Emmanuel Delahaye

            #6
            Re: strtol library function

            In 'comp.lang.c', David Scarlett <look@my.signat ure> wrote:
            [color=blue]
            > Is behaviour defined when the first argument of strtol is NULL?[/color]

            No.
            [color=blue]
            > And if the string contains only digits, is the 2nd argument (assuming
            > it wasn't NULL) guaranteed to be set to a pointer to a pointer to '\0'?[/color]

            The argument is unchanged. It's the pointed value that is changed to a
            pointer to '\0'.

            --
            -ed- get my email here: http://marreduspam.com/ad672570
            The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
            C-reference: http://www.dinkumware.com/manuals/reader.aspx?lib=c99
            FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/

            Comment

            • Paul Emmons

              #7
              Re: strtol library function

              On Sat, 15 May 2004 12:51:16 -0400, Al Bowers <xabowers@rapid sys.com>
              wrote:
              [color=blue]
              >Yeah! That is the way I always used it. But recently someone mentioned
              >that there is nothing to prevent the a function implementation from
              >setting errno to ERANGE even if the resulting value is in the range
              >of a long. I looked at the Standard's description of the function and
              >saw that it id say the an unrepresentable value would result in both
              >errno == ERANGE and the return value either LONG_MAX or LONG_MIN.[/color]

              Perhaps that was in the course of my stupid question about strtoul(),
              which I erroneously claimed that it returned ERANGE for input between
              LONG_MAX and ULONG_MAX. It doesn't. I was actually playing with
              strtol() instead, and didn't realize it.

              It still seems rather strange to me that strtoul() accepts "-2" and
              returns 4294967294 without complaint, but according to the man page it
              is supposed to do so.

              Comment

              • jacob navia

                #8
                Re: strtol library function


                "Paul Emmons" <pemmons@voicen et.com> a écrit dans le message de
                news:ddhfa0tlc0 4tf6a7lkvk35e43 b2harrnt3@4ax.c om...[color=blue]
                > On Sat, 15 May 2004 12:51:16 -0400, Al Bowers <xabowers@rapid sys.com>
                > wrote:[/color]
                [snip][color=blue]
                >
                > It still seems rather strange to me that strtoul() accepts "-2" and
                > returns 4294967294 without complaint, but according to the man page it
                > is supposed to do so.
                >[/color]

                But HOW is that function supposed to know that
                it was passed -2 and NOT 4294967294 ????

                The binary representation of both is exactly the same!



                Comment

                • Ben Pfaff

                  #9
                  Re: strtol library function

                  "jacob navia" <jacob@jacob.re mcomp.fr> writes:
                  [color=blue]
                  > "Paul Emmons" <pemmons@voicen et.com> a écrit dans le message de
                  > news:ddhfa0tlc0 4tf6a7lkvk35e43 b2harrnt3@4ax.c om...[color=green]
                  >> On Sat, 15 May 2004 12:51:16 -0400, Al Bowers <xabowers@rapid sys.com>
                  >> wrote:[/color]
                  > [snip][color=green]
                  >>
                  >> It still seems rather strange to me that strtoul() accepts "-2" and
                  >> returns 4294967294 without complaint, but according to the man page it
                  >> is supposed to do so.[/color]
                  >
                  > But HOW is that function supposed to know that
                  > it was passed -2 and NOT 4294967294 ????[/color]

                  Because the first character of the string passed to it, following
                  whitespace is '-', or some locale-specific version thereof.
                  [color=blue]
                  > The binary representation of both is exactly the same![/color]

                  Their text representations differ.

                  Perhaps you should join Richard Heathfield's "reading for
                  comprehension" class.
                  --
                  Peter Seebach on C99:
                  "[F]or the most part, features were added, not removed. This sounds
                  great until you try to carry a full-sized printout of the standard
                  around for a day."

                  Comment

                  • those who know me have no need of my name

                    #10
                    Re: strtol library function

                    in comp.lang.c i read:
                    [color=blue]
                    >It still seems rather strange to me that strtoul() accepts "-2" and
                    >returns 4294967294 without complaint, but according to the man page it
                    >is supposed to do so.[/color]

                    for the same reason that assigning -2 to an unsigned long results in that
                    value.

                    --
                    a signature

                    Comment

                    • Dan Pop

                      #11
                      Re: strtol library function

                      In <m1zn87emj6.gnu s@usa.net> those who know me have no need of my name <not-a-real-address@usa.net > writes:
                      [color=blue]
                      >in comp.lang.c i read:
                      >[color=green]
                      >>It still seems rather strange to me that strtoul() accepts "-2" and
                      >>returns 4294967294 without complaint, but according to the man page it
                      >>is supposed to do so.[/color]
                      >
                      >for the same reason that assigning -2 to an unsigned long results in that
                      >value.[/color]

                      If it's the same reason, then when should strtoul() set errno to ERANGE
                      and why?

                      Dan
                      --
                      Dan Pop
                      DESY Zeuthen, RZ group
                      Email: Dan.Pop@ifh.de

                      Comment

                      • Dan Pop

                        #12
                        Re: strtol library function

                        In <ddhfa0tlc04tf6 a7lkvk35e43b2ha rrnt3@4ax.com> Paul Emmons <pemmons@voicen et.com> writes:
                        [color=blue]
                        >It still seems rather strange to me that strtoul() accepts "-2" and
                        >returns 4294967294 without complaint, but according to the man page it
                        >is supposed to do so.[/color]

                        According to my reading of the C standard it isn't supposed to do so:

                        Returns

                        8 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.

                        Unless I'm missing something, -2 is outside the range of values that can
                        be represented by unsigned long. And any argument about "folding" it into
                        range would equally apply to values above ULONG_MAX, so this function
                        should NEVER set errno to ERANGE if that argument applied.

                        However, I know, from past discussions, that committee members disagree
                        with my interpretation.

                        Dan
                        --
                        Dan Pop
                        DESY Zeuthen, RZ group
                        Email: Dan.Pop@ifh.de

                        Comment

                        • Al Bowers

                          #13
                          Re: strtol library function



                          Dan Pop wrote:[color=blue]
                          > In <ddhfa0tlc04tf6 a7lkvk35e43b2ha rrnt3@4ax.com> Paul Emmons <pemmons@voicen et.com> writes:
                          >
                          >[color=green]
                          >>It still seems rather strange to me that strtoul() accepts "-2" and
                          >>returns 4294967294 without complaint, but according to the man page it
                          >>is supposed to do so.[/color]
                          >
                          >
                          > According to my reading of the C standard it isn't supposed to do so:
                          >
                          > Returns
                          >
                          > 8 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.
                          >
                          > Unless I'm missing something, -2 is outside the range of values that can
                          > be represented by unsigned long. And any argument about "folding" it into
                          > range would equally apply to values above ULONG_MAX, so this function
                          > should NEVER set errno to ERANGE if that argument applied.
                          >[/color]

                          I don't have the previous standard available but I believe the wording
                          for the description for strtoul was "If the subject sequence field
                          begins witha a minus sign, the value resulting from the conversion is
                          negated". Thus the function would first convert "-2" to, for example,
                          unsigned long x = 2, then it would be negated, x = -x. I can accept that
                          but I don't see this wording in the current standard, which, at least to
                          me, is confusing.

                          On a related issue, the current standard seems to support symmetry
                          between functions *scanf's and strotol, at least in regards to the
                          format of the expected subject sequence, yet doesn't show any support
                          for the symmetry in the resulting value. Most of the implementions that
                          I use, sscanf and strtoul will yield different results for the string
                          sequence "-2".

                          --
                          Al Bowers
                          Tampa, Fl USA
                          mailto: xabowers@myrapi dsys.com (remove the x to send email)
                          Latest news coverage, email, free stock quotes, live scores and video are just the beginning. Discover more every day at Yahoo!


                          Comment

                          • Al Bowers

                            #14
                            Re: strtol library function



                            Al Bowers wrote:

                            [color=blue]
                            >
                            > On a related issue, the current standard seems to support symmetry
                            > between functions *scanf's and strotol, at least in regards to the
                            > format of the expected subject sequence, yet doesn't show any support
                            > for the symmetry in the resulting value. Most of the implementions that
                            > I use, sscanf and strtoul will yield different results for the string
                            > sequence "-2".[/color]

                            Oops, I meant "will yield a different result for the string
                            "4294967297 ", where the results are 4294967295 (ULONG_MAX) with
                            strtoul and 1 with sscanf.

                            --
                            Al Bowers
                            Tampa, Fl USA
                            mailto: xabowers@myrapi dsys.com (remove the x to send email)
                            Latest news coverage, email, free stock quotes, live scores and video are just the beginning. Discover more every day at Yahoo!


                            Comment

                            • CBFalconer

                              #15
                              Re: strtol library function

                              Dan Pop wrote:[color=blue]
                              >[/color]
                              .... snip ...[color=blue]
                              >
                              > Unless I'm missing something, -2 is outside the range of values
                              > that can be represented by unsigned long. And any argument about
                              > "folding" it into range would equally apply to values above
                              > ULONG_MAX, so this function should NEVER set errno to ERANGE if
                              > that argument applied.[/color]

                              We had this argument right here and on comp.std.c a month or so
                              ago, and I took your side. I started it when I found that the
                              DJGPP implementation of strtoul did that quiet conversion. At any
                              rate, I got outshouted, and beaten into agreement that the action
                              was required by the standard.

                              I still think it is wrong, wrong, wrong, in fact wrong.

                              --
                              Chuck F (cbfalconer@yah oo.com) (cbfalconer@wor ldnet.att.net)
                              Available for consulting/temporary embedded and systems.
                              <http://cbfalconer.home .att.net> USE worldnet address!


                              Comment

                              Working...