what is atoi( )

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

    #31
    Re: what is atoi( )

    David Paleino wrote:[color=blue]
    >
    > Mark McIntyre ha scritto:[color=green]
    > > On Fri, 03 Mar 2006 12:22:32 +0100, in comp.lang.c , David Paleino
    > > <d.paleino@gmai l.com> wrote:
    > >
    > >[color=darkred]
    > >>Yes, I'm using gcc, the problem is that I can't really find it:[/color]
    > >
    > >
    > > You really don't need to.
    > >
    > >[color=darkred]
    > >>As you can see, at least in /usr/include, there isn't a single
    > >>definition for __strtol_intern al.[/color]
    > >
    > >
    > > *shrug*. It could be internal to the compiler binary, or inside some
    > > library, or whatever.
    > > How gcc implements the fn is entirely up to it.
    > >[/color]
    >
    > Sure, but it was just curiosity. I don't need it. No one ever will. ;)
    > It was just to understand how the atoi() function was implemented :D[/color]

    a_toi does what atoi does, for valid arguments.

    int a_toi(const char *nptr)
    {
    int n;

    n = 0;
    while (isspace(*nptr) ) {
    ++nptr;
    }
    if (*nptr != '-') {
    if (*nptr == '+') {
    ++nptr;
    }
    while (isdigit(*nptr) ) {
    n = 10 * n - '0' + *nptr++;
    }
    } else {
    ++nptr;
    while (isdigit(*nptr) ) {
    n = 10 * n + '0' - *nptr++;
    }
    }
    return n;
    }

    --
    pete

    Comment

    • Micah Cowan

      #32
      Re: what is atoi( )

      Richard Heathfield <invalid@invali d.invalid> writes:
      [color=blue]
      > Vladimir S. Oka said:
      >[color=green]
      > >
      > > Richard Heathfield wrote:[color=darkred]
      > >> Sunil Varma said:
      > >>
      > >> > The return value is 0 if the input cannot be converted to a value of
      > >> > that type.
      > >>
      > >> Chapter and verse please.[/color]
      > >
      > > Is my reading of the Standrad correct in the sense that the error
      > > return of `atoi` and friends is actually not specified (apart from
      > > saying that it may differ from `strtoul`)?[/color]
      >
      > 4.10.1 String conversion functions
      >
      > The functions atof , atoi , and atol need not affect the value of
      > the integer expression errno on an error. If the value of the result
      > cannot be represented, the behavior is undefined.
      >
      >
      > Now, how do you represent "the value of the result" if the call looks like
      > this?
      >
      > int greeting = atoi("Hello, world!");
      >
      > Whether this is an error would appear to depend on how "error" is defined
      > with respect to strtol - and the Standard doesn't even /use/ the word
      > "error" in the strtol section.
      >
      > Having said that, the strtol section does explain that "Hello, world!" would
      > be parsed with an empty "subject sequence" (if the base is 10, as it would
      > be in this case), and 0 is returned.
      >
      > So I guess it all depends on what you mean - or rather, what the Standard
      > means - by "error".[/color]

      Well the only error condition actually defined for ato*() is in the
      case that "the value of the result" (as I read it, of the implied
      strto*()) cannot be represented. I would expect any implementation to
      produce 0 for atoi("Hello, world!");. But I admit that the language is
      a little shakey.

      Now, what should I expect for atoi("100000000 000000000000000 00000")? :-)

      Comment

      • CBFalconer

        #33
        Re: what is atoi( )

        Micah Cowan wrote:[color=blue]
        >[/color]
        .... snip ...[color=blue]
        >
        > Now, what should I expect for atoi("100000000 000000000000000 00000")? :-)[/color]

        If that value is greater than INT_MAX you get undefined (or
        possibly implementation defined) behaviour. Look it up.

        --
        "If you want to post a followup via groups.google.c om, don't use
        the broken "Reply" link at the bottom of the article. Click on
        "show options" at the top of the article, then click on the
        "Reply" at the bottom of the article headers." - Keith Thompson
        More details at: <http://cfaj.freeshell. org/google/>
        Also see <http://www.safalra.com/special/googlegroupsrep ly/>


        Comment

        • CBFalconer

          #34
          Re: what is atoi( )

          sudharsan wrote:[color=blue]
          >
          > could you please explain wat atoi( ) function is for and an example
          > how to use it?[/color]

          Its purpose is to confuse newbies and discourage proper testing for
          input errors. Anyone with a modicum of knowledge would use
          something in the strto*() family instead.

          --
          "If you want to post a followup via groups.google.c om, don't use
          the broken "Reply" link at the bottom of the article. Click on
          "show options" at the top of the article, then click on the
          "Reply" at the bottom of the article headers." - Keith Thompson
          More details at: <http://cfaj.freeshell. org/google/>
          Also see <http://www.safalra.com/special/googlegroupsrep ly/>


          Comment

          • Randy Howard

            #35
            Re: what is atoi( )

            CBFalconer wrote
            (in article <440910CF.38C96 5D9@yahoo.com>) :
            [color=blue]
            > sudharsan wrote:[color=green]
            >>
            >> could you please explain wat atoi( ) function is for and an example
            >> how to use it?[/color]
            >
            > Its purpose is to confuse newbies and discourage proper testing for
            > input errors. Anyone with a modicum of knowledge would use
            > something in the strto*() family instead.[/color]

            By far the best answer in this thread.


            --
            Randy Howard (2reply remove FOOBAR)
            "The power of accurate observation is called cynicism by those
            who have not got it." - George Bernard Shaw





            Comment

            • Ben Bacarisse

              #36
              Re: what is atoi( )

              On Fri, 03 Mar 2006 23:00:15 -0500, CBFalconer wrote:
              [color=blue]
              > sudharsan wrote:[color=green]
              >>
              >> could you please explain wat atoi( ) function is for and an example how
              >> to use it?[/color]
              >
              > Its purpose is to confuse newbies and discourage proper testing for input
              > errors. Anyone with a modicum of knowledge would use something in the
              > strto*() family instead.[/color]

              Maybe a little bit harsh. I agree with the "confusing newbies" part, but
              there are cases like inside lex/flex rules where atoi is a reasonable
              choice since you know what the string has in it.

              --
              Ben.

              Comment

              • Randy Howard

                #37
                Re: what is atoi( )

                Ben Bacarisse wrote
                (in article <pan.2006.03.05 .14.52.06.78812 7@bsb.me.uk>):
                [color=blue]
                > On Fri, 03 Mar 2006 23:00:15 -0500, CBFalconer wrote:
                >[color=green]
                >> sudharsan wrote:[color=darkred]
                >>>
                >>> could you please explain wat atoi( ) function is for and an example how
                >>> to use it?[/color]
                >>
                >> Its purpose is to confuse newbies and discourage proper testing for input
                >> errors. Anyone with a modicum of knowledge would use something in the
                >> strto*() family instead.[/color]
                >
                > Maybe a little bit harsh. I agree with the "confusing newbies" part, but
                > there are cases like inside lex/flex rules where atoi is a reasonable
                > choice since you know what the string has in it.[/color]

                Okay, I have a function that can be used safely in a few narrow,
                constricted input cases. I have another function that can be
                used in all cases. Why would I bother with the former?

                --
                Randy Howard (2reply remove FOOBAR)
                "The power of accurate observation is called cynicism by those
                who have not got it." - George Bernard Shaw





                Comment

                • Mark McIntyre

                  #38
                  Re: what is atoi( )

                  On Sun, 05 Mar 2006 19:09:47 GMT, in comp.lang.c , Randy Howard
                  <randyhoward@FO OverizonBAR.net > wrote:
                  [color=blue]
                  >Okay, I have a function that can be used safely in a few narrow,
                  >constricted input cases. I have another function that can be
                  >used in all cases. Why would I bother with the former?[/color]

                  int unsafe_conversi on(char*) { return do_stuff_fast() ;}

                  int safe_conversion (char*, int, int*) { return very_slow();}

                  Mark McIntyre
                  --
                  "Debugging is twice as hard as writing the code in the first place.
                  Therefore, if you write the code as cleverly as possible, you are,
                  by definition, not smart enough to debug it."
                  --Brian Kernighan

                  ----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
                  http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
                  ----= East and West-Coast Server Farms - Total Privacy via Encryption =----

                  Comment

                  • Randy Howard

                    #39
                    Re: what is atoi( )

                    Mark McIntyre wrote
                    (in article <1cfm02ptp91up4 fc37oe0rfb21esu uaq4u@4ax.com>) :
                    [color=blue]
                    > On Sun, 05 Mar 2006 19:09:47 GMT, in comp.lang.c , Randy Howard
                    > <randyhoward@FO OverizonBAR.net > wrote:
                    >[color=green]
                    >> Okay, I have a function that can be used safely in a few narrow,
                    >> constricted input cases. I have another function that can be
                    >> used in all cases. Why would I bother with the former?[/color]
                    >
                    > int unsafe_conversi on(char*) { return do_stuff_fast() ;}
                    >
                    > int safe_conversion (char*, int, int*) { return very_slow();}[/color]

                    I almost put in a "apart from performance reasons" in the
                    original, and now I wish I had. I'm sort of wondering whether
                    the "newbies" referenced originally care about that at all
                    though. They're probably more concerned with getting the right
                    answer on their homework.


                    --
                    Randy Howard (2reply remove FOOBAR)
                    "The power of accurate observation is called cynicism by those
                    who have not got it." - George Bernard Shaw





                    Comment

                    • Ben Bacarisse

                      #40
                      Re: what is atoi( )

                      On Sun, 05 Mar 2006 19:09:47 +0000, Randy Howard wrote:
                      [color=blue]
                      > Ben Bacarisse wrote
                      > (in article <pan.2006.03.05 .14.52.06.78812 7@bsb.me.uk>):
                      >[color=green]
                      >> On Fri, 03 Mar 2006 23:00:15 -0500, CBFalconer wrote:
                      >>[color=darkred]
                      >>> sudharsan wrote:
                      >>>>
                      >>>> could you please explain wat atoi( ) function is for and an example
                      >>>> how to use it?
                      >>>
                      >>> Its purpose is to confuse newbies and discourage proper testing for
                      >>> input errors. Anyone with a modicum of knowledge would use something
                      >>> in the strto*() family instead.[/color]
                      >>
                      >> Maybe a little bit harsh. I agree with the "confusing newbies" part,
                      >> but there are cases like inside lex/flex rules where atoi is a
                      >> reasonable choice since you know what the string has in it.[/color]
                      >
                      > Okay, I have a function that can be used safely in a few narrow,
                      > constricted input cases. I have another function that can be used in all
                      > cases. Why would I bother with the former?[/color]

                      A rhetorical question, presumably, since you know as well as I that there
                      is no reason for you to bother with any function whose behaviour is
                      covered by a more general one (if adequate performance is included in the
                      definition of a function's behaviour).

                      I gave a case where it was "reasonable " -- no more -- certainly not
                      preferable. I thought CBF's characterisatio n of its use as evidence of
                      not having "a modicum of knowledge" was rather harsh. That is all.

                      Its use in lex rules crops up in several articles by Mike Lesk. I would
                      not conclude from that that he lacks a modicum of knowledge. I do not
                      claim that as the author of lex and as a "name" his examples should be
                      seen as perfect, nor even that they would not be improved by replacing
                      atoi with strtoul/strtod, just that the use is reasonable and does not
                      demonstrate ignorance.

                      --
                      Ben.

                      Comment

                      • pete

                        #41
                        Re: what is atoi( )

                        Ben Bacarisse wrote:[color=blue]
                        >
                        > On Sun, 05 Mar 2006 19:09:47 +0000, Randy Howard wrote:
                        >[color=green]
                        > > Ben Bacarisse wrote
                        > > (in article <pan.2006.03.05 .14.52.06.78812 7@bsb.me.uk>):
                        > >[color=darkred]
                        > >> On Fri, 03 Mar 2006 23:00:15 -0500, CBFalconer wrote:
                        > >>
                        > >>> sudharsan wrote:
                        > >>>>
                        > >>>> could you please explain wat atoi( ) function is for and an example
                        > >>>> how to use it?
                        > >>>
                        > >>> Its purpose is to confuse newbies and discourage proper testing for
                        > >>> input errors. Anyone with a modicum of knowledge would use something
                        > >>> in the strto*() family instead.
                        > >>
                        > >> Maybe a little bit harsh. I agree with the "confusing newbies" part,
                        > >> but there are cases like inside lex/flex rules where atoi is a
                        > >> reasonable choice since you know what the string has in it.[/color]
                        > >
                        > > Okay, I have a function that can be used safely in a few narrow,
                        > > constricted input cases.
                        > > I have another function that can be used in all
                        > > cases. Why would I bother with the former?[/color]
                        >
                        > A rhetorical question, presumably,
                        > since you know as well as I that there
                        > is no reason for you to bother with any function whose behaviour is
                        > covered by a more general one
                        > (if adequate performance is included in the
                        > definition of a function's behaviour).[/color]

                        Adequate performance isn't included in the
                        definition of any standard function's behaviour.

                        There are many standard functions whose behavior
                        is covered by more general ones.

                        I once advised a colleague
                        on how to shrink the size of his embedded program
                        by replacing all his fprintf calls, with fputs calls.

                        --
                        pete

                        Comment

                        • pete

                          #42
                          Is atoi(&quot;&quo t;) equal to zero or is it undefined.

                          John Smith wrote:[color=blue]
                          >
                          > pete wrote:[color=green]
                          > > pete wrote:
                          > >[color=darkred]
                          > >>Richard Heathfield wrote:
                          > >>
                          > >>>Ron Lima said:
                          > >>>
                          > >>>
                          > >>>>If the string cannot be converted to a number, at all, atoi
                          > >>>>returns zero.
                          > >>>
                          > >>>Chapter and verse, please.
                          > >>
                          > >>It depends on whether "no conversion" is the same thing as "error".
                          > >>Is atoi(""), an error?[/color][/color]
                          >
                          > from Harbison & Steele, p.411:
                          >
                          > "If the functions in this section (i.e. atox() family) are unable
                          > to convert the input string, then their behavior is undefined."[/color]

                          I think atoi("") is equal to zero.

                          The standard says this about atoi:
                          Except for
                          the behavior on error, they are equivalent to
                          atoi: (int)strtol(npt r, (char **)NULL, 10)

                          and this about strtol:
                          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.

                          Those words make me think that attempting to convert
                          a string to an out of range integer, is an error,
                          and that attempting to convert a nonconvertable string,
                          is not an error.

                          --
                          pete

                          Comment

                          • David R Tribble

                            #43
                            Re: Is atoi(&quot;&quo t;) equal to zero or is it undefined.

                            pete wrote:[color=blue]
                            > I think atoi("") is equal to zero.
                            >
                            > The standard says this about atoi:
                            > Except for
                            > the behavior on error, they are equivalent to
                            > atoi: (int)strtol(npt r, (char **)NULL, 10)
                            >
                            > and this about strtol:
                            > 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.
                            >
                            > Those words make me think that attempting to convert
                            > a string to an out of range integer, is an error,
                            > and that attempting to convert a nonconvertable string,
                            > is not an error.[/color]

                            Yes, which would also cover cases like atoi("xyz").

                            Note that an implementation is free to set errno to some nonzero
                            value as well (just don't count on it).

                            -drt

                            Comment

                            • Arndt Jonasson

                              #44
                              Re: what is atoi( )


                              "Vladimir S. Oka" <novine@btopenw orld.com> writes:[color=blue]
                              > Arndt Jonasson wrote:
                              >[color=green]
                              > >
                              > > "Vladimir S. Oka" <novine@btopenw orld.com> writes:[color=darkred]
                              > >> David Paleino wrote:
                              > >> > [...]
                              > >> > Now, looking into stdlib.h, I see that strtol() refers to
                              > >> > __strtol_intern al(), but I can't find it:
                              > >> >
                              > >> > "
                              > >> > extern __inline long int __NTH (strtol (__const char *__restrict
                              > >> > __nptr, char **__restrict __endptr, int __base))
                              > >> > {
                              > >> > return __strtol_intern al (__nptr, __endptr, __base, 0);
                              > >> > }
                              > >> > "
                              > >> >
                              > >> > Does anyone have a slight idea where __strtol_intern al is placed?
                              > >>
                              > >> It is placed in the standard library implementation. You may not have
                              > >> access to the source code for it (you might if you're using gcc).
                              > >> Even if you had, it's not guaranteed to be in C, or any other
                              > >> language you can think of.[/color]
                              > >
                              > > Does __strtol_intern al even have to be a function? Isn't it the case
                              > > that the compiler is allowed to do some appropriate optimization,
                              > > having complete knowledge about the semantics of the call? (For
                              > > example, partially unroll a loop - maybe 'strtol' isn't the best
                              > > candidate, though.)[/color]
                              >
                              > C Standard really does not care how atoi and friends are implemented, as
                              > long as they do as Standard requires. They might as well send carrier
                              > pigeons to Egypt. I'm not familiar enough with any Standard C library
                              > implementation to tell you how it's "usually" done. Anyone?[/color]

                              I didn't mean just any implementation of 'atoi', though I probably
                              didn't express myself clearly enough. I meant this particular one,
                              where a header file in the implementation contains what seems to be a
                              call to a function in the implementation' s reserved name space. I wondered
                              whether that function must actually exist as a function, or if this is
                              allowed to be, so to speak, private communication to the compiler, and
                              the actual implementation need not be expressible in standard C at all.

                              Comment

                              • Arndt Jonasson

                                #45
                                Re: what is atoi( )


                                CBFalconer <cbfalconer@yah oo.com> writes:[color=blue]
                                > sudharsan wrote:[color=green]
                                > >
                                > > could you please explain wat atoi( ) function is for and an example
                                > > how to use it?[/color]
                                >
                                > Its purpose is to confuse newbies and discourage proper testing for
                                > input errors. Anyone with a modicum of knowledge would use
                                > something in the strto*() family instead.[/color]

                                My habit has been to use 'atoi' when I need to convert a command line
                                argument to a number, and the number must be larger than 0. Then the
                                code looks like

                                if ((x = atoi(arg)) == 0)
                                usage();

                                Using 'strtol', it seems I have to do:

                                errno = 0;
                                x = strtol(arg, NULL, 10);
                                if (errno != 0)
                                usage();

                                though I do get the advantage that overflow is detected. If 0 is
                                allowed input, using 'strtol' also needs a check whether the input
                                string is empty, since it treats "" as valid (why was this considered
                                a good idea?). I have usually used 'sscanf' in this case.

                                My quibble doesn't amount to much, since I can't recommend a function
                                that doesn't detect malformed input before one that does, and the portion
                                of a program that deals with command line arguments is usually very small
                                compared to the rest.

                                Comment

                                Working...