atoi return

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

    atoi return

    I have just read atoi() returns no errors. It returns an int though and
    the value of the int is supposed to be the value of the conversion. It seems
    to me that right there tells you if there was success or not. Am I wrong?

    Bill


  • Ian Collins

    #2
    Re: atoi return

    Bill Cunningham wrote:
    I have just read atoi() returns no errors. It returns an int though and
    the value of the int is supposed to be the value of the conversion. It seems
    to me that right there tells you if there was success or not. Am I wrong?
    >
    Wasn't this all beaten to death on one of your threads a week or two back?

    What value would indicate failure?

    --
    Ian Collins

    Comment

    • Richard

      #3
      Re: atoi return


      "Bill Cunningham" <nospam@nspam.i nvalidwrites:
      I have just read atoi() returns no errors. It returns an int though and
      the value of the int is supposed to be the value of the conversion. It seems
      to me that right there tells you if there was success or not. Am I wrong?
      >
      Bill
      What you should do is use sprintf and scanf and then do a string compare
      with their output to your atoi output. If the result is the same then
      you are on the right track.

      Comment

      • Ian Collins

        #4
        Re: atoi return

        Richard wrote:
        "Bill Cunningham" <nospam@nspam.i nvalidwrites:
        >
        > I have just read atoi() returns no errors. It returns an int though and
        >the value of the int is supposed to be the value of the conversion. It seems
        >to me that right there tells you if there was success or not. Am I wrong?
        >>
        >Bill
        >
        What you should do is use sprintf and scanf and then do a string compare
        with their output to your atoi output. If the result is the same then
        you are on the right track.
        >
        Are you baiting pool Bill, or posting bollocks?

        --
        Ian Collins

        Comment

        • Keith Thompson

          #5
          Re: atoi return

          "Bill Cunningham" <nospam@nspam.i nvalidwrites:
          I have just read atoi() returns no errors. It returns an int though and
          the value of the int is supposed to be the value of the conversion. It seems
          to me that right there tells you if there was success or not. Am I wrong?
          Yes.

          Take a look at the following program and tell me how you'd detect an
          error in a call to atoi().

          #include <stdio.h>
          #include <stdlib.h>
          int main(void)
          {
          char *good = "0";
          char *bad = "bad";
          printf("atoi(\" %s\") = %d\n", good, atoi(good));
          printf("atoi(\" %s\") = %d\n", bad, atoi(bad));
          return 0;
          }

          --
          Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
          Nokia
          "We must do something. This is something. Therefore, we must do this."
          -- Antony Jay and Jonathan Lynn, "Yes Minister"

          Comment

          • Bill Cunningham

            #6
            Re: atoi return


            "Keith Thompson" <kst-u@mib.orgwrote in message
            news:ln4p3lm0yy .fsf@nuthaus.mi b.org...

            Yes.
            >
            Take a look at the following program and tell me how you'd detect an
            error in a call to atoi().
            >
            #include <stdio.h>
            #include <stdlib.h>
            int main(void)
            {
            char *good = "0";
            char *bad = "bad";
            printf("atoi(\" %s\") = %d\n", good, atoi(good));
            printf("atoi(\" %s\") = %d\n", bad, atoi(bad));
            return 0;
            }
            I can't really read that Keith but if I remember right this was
            mentioned before but never really explained. I would've tried something
            like this if it's even valid.
            ....
            int i;
            if (i=atoi(argv[1]))!=sizeof(int) )
            fprintf(stderr, "error\n");

            *sigh* I dunno. Does this make any sense?

            Bill



            Comment

            • Keith Thompson

              #7
              Re: atoi return

              "Bill Cunningham" <nospam@nspam.i nvalidwrites:
              "Keith Thompson" <kst-u@mib.orgwrote in message
              news:ln4p3lm0yy .fsf@nuthaus.mi b.org...
              >Take a look at the following program and tell me how you'd detect an
              >error in a call to atoi().
              >>
              >#include <stdio.h>
              >#include <stdlib.h>
              >int main(void)
              >{
              > char *good = "0";
              > char *bad = "bad";
              > printf("atoi(\" %s\") = %d\n", good, atoi(good));
              > printf("atoi(\" %s\") = %d\n", bad, atoi(bad));
              > return 0;
              >}
              >
              I can't really read that Keith but if I remember right this was
              mentioned before but never really explained. I would've tried something
              like this if it's even valid.
              ...
              int i;
              if (i=atoi(argv[1]))!=sizeof(int) )
              fprintf(stderr, "error\n");
              >
              *sigh* I dunno. Does this make any sense?
              No, it doesn't. What does sizeof(int) have to do with anything?

              Yes, this was mentioned before, and it was explained in great detail.

              Here's the point. If you give atoi() a string that doesn't represent
              a number, such as the string "bad", it returns 0. If you give it the
              string "0", which does represent a number it returns 0. If atoi()
              returns 0, you *can't tell* whether it successfully converted the
              string "0" or failed to convert the string "bad".

              Even worse, if atoi() is given a string that represents a number
              that's too big to hold in an int, it invokes undefined behavior. For
              example, there's no telling what atoi("999999999 99999999999") will do;
              it could crash your program or worse.

              --
              Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
              Nokia
              "We must do something. This is something. Therefore, we must do this."
              -- Antony Jay and Jonathan Lynn, "Yes Minister"

              Comment

              • Bill Cunningham

                #8
                Re: atoi return


                "Keith Thompson" <kst-u@mib.orgwrote in message
                news:ln1vyokqw1 .fsf@nuthaus.mi b.org...

                [snip]
                Here's the point. If you give atoi() a string that doesn't represent
                a number, such as the string "bad", it returns 0. If you give it the
                string "0", which does represent a number it returns 0. If atoi()
                returns 0, you *can't tell* whether it successfully converted the
                string "0" or failed to convert the string "bad".
                >
                Even worse, if atoi() is given a string that represents a number
                that's too big to hold in an int, it invokes undefined behavior. For
                example, there's no telling what atoi("999999999 99999999999") will do;
                it could crash your program or worse.
                I see. Must be a left over dinosaur like gets().

                Bill


                Comment

                • Keith Thompson

                  #9
                  Re: atoi return

                  "Bill Cunningham" <nospam@nspam.i nvalidwrites:
                  "Keith Thompson" <kst-u@mib.orgwrote in message
                  news:ln1vyokqw1 .fsf@nuthaus.mi b.org...
                  >
                  [snip]
                  >
                  >Here's the point. If you give atoi() a string that doesn't represent
                  >a number, such as the string "bad", it returns 0. If you give it the
                  >string "0", which does represent a number it returns 0. If atoi()
                  >returns 0, you *can't tell* whether it successfully converted the
                  >string "0" or failed to convert the string "bad".
                  >>
                  >Even worse, if atoi() is given a string that represents a number
                  >that's too big to hold in an int, it invokes undefined behavior. For
                  >example, there's no telling what atoi("999999999 99999999999") will do;
                  >it could crash your program or worse.
                  >
                  I see. Must be a left over dinosaur like gets().
                  Pretty much. It's not nearly as dangerous as gets, though; since its
                  behavior depends on the argument you pass to it (which you can check),
                  not on what appears on stdin (over which you typically have no
                  control), it *can* be used safely. But by the time you add code to
                  verify the argument before calling atoi(), you might as well call
                  strtol().

                  --
                  Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                  Nokia
                  "We must do something. This is something. Therefore, we must do this."
                  -- Antony Jay and Jonathan Lynn, "Yes Minister"

                  Comment

                  • CBFalconer

                    #10
                    Re: atoi return

                    Bill Cunningham wrote:
                    "Keith Thompson" <kst-u@mib.orgwrote in message
                    >
                    .... snip ...
                    >
                    >Even worse, if atoi() is given a string that represents a number
                    >that's too big to hold in an int, it invokes undefined behavior.
                    >For example, there's no telling what atoi("999999999 99999999999")
                    >will do; it could crash your program or worse.
                    >
                    I see. Must be a left over dinosaur like gets().
                    No. gets() is just unsafe regardless. atoi() can be used safely,
                    but why bother when you have strtol available.

                    --
                    [mail]: Chuck F (cbfalconer at maineline dot net)
                    [page]: <http://cbfalconer.home .att.net>
                    Try the download section.

                    Comment

                    • Richard Heathfield

                      #11
                      Re: atoi return

                      CBFalconer said:
                      Bill Cunningham wrote:
                      >"Keith Thompson" <kst-u@mib.orgwrote in message
                      >>
                      ... snip ...
                      >>
                      >>Even worse, if atoi() is given a string that represents a number
                      >>that's too big to hold in an int, it invokes undefined behavior.
                      >>For example, there's no telling what atoi("999999999 99999999999")
                      >>will do; it could crash your program or worse.
                      >>
                      >I see. Must be a left over dinosaur like gets().
                      >
                      No. gets() is just unsafe regardless. atoi() can be used safely,
                      but why bother when you have strtol available.
                      In other words, it's a left over dinosaur. And so is gets(). Therefore,
                      it's a left over dinosaur like gets(). Which is what he said.

                      --
                      Richard Heathfield <http://www.cpax.org.uk >
                      Email: -http://www. +rjh@
                      Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
                      "Usenet is a strange place" - dmr 29 July 1999

                      Comment

                      • Richard Heathfield

                        #12
                        Re: atoi return

                        Keith Thompson said:

                        <snip>
                        Even worse, if atoi() is given a string that represents a number
                        that's too big to hold in an int, it invokes undefined behavior. For
                        example, there's no telling what atoi("999999999 99999999999") will do;
                        it could crash your program or worse.
                        Unless INT_MAX >= 999999999999999 99999, obviously.

                        --
                        Richard Heathfield <http://www.cpax.org.uk >
                        Email: -http://www. +rjh@
                        Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
                        "Usenet is a strange place" - dmr 29 July 1999

                        Comment

                        • CBFalconer

                          #13
                          Re: atoi return

                          Richard Heathfield wrote:
                          CBFalconer said:
                          >Bill Cunningham wrote:
                          >>"Keith Thompson" <kst-u@mib.orgwrote in message
                          >>>
                          >... snip ...
                          >>>
                          >>>Even worse, if atoi() is given a string that represents a number
                          >>>that's too big to hold in an int, it invokes undefined behavior.
                          >>>For example, there's no telling what atoi("999999999 99999999999")
                          >>>will do; it could crash your program or worse.
                          >>>
                          >>I see. Must be a left over dinosaur like gets().
                          >>
                          >No. gets() is just unsafe regardless. atoi() can be used safely,
                          >but why bother when you have strtol available.
                          >
                          In other words, it's a left over dinosaur. And so is gets().
                          Therefore, it's a left over dinosaur like gets(). Which is what he
                          said.
                          Not so. gets should never appear in any code. atoi can. One
                          objective of the C standard is to preserve the viability of old
                          code.

                          I can also see various other differences between gets and atoi. So
                          the above comment is simply a waste of time.

                          --
                          [mail]: Chuck F (cbfalconer at maineline dot net)
                          [page]: <http://cbfalconer.home .att.net>
                          Try the download section.

                          Comment

                          • vippstar@gmail.com

                            #14
                            Re: atoi return

                            On Oct 11, 4:59 pm, CBFalconer <cbfalco...@yah oo.comwrote:
                            Richard Heathfield wrote:
                            CBFalconer said:
                            No. gets() is just unsafe regardless. atoi() can be used safely,
                            but why bother when you have strtol available.
                            >
                            In other words, it's a left over dinosaur. And so is gets().
                            Therefore, it's a left over dinosaur like gets(). Which is what he
                            said.
                            >
                            Not so. gets should never appear in any code. atoi can. One
                            objective of the C standard is to preserve the viability of old
                            code.
                            gets(stdout); is safe I believe, just like atoi("123"); is safe.

                            Comment

                            • James Kuyper

                              #15
                              Re: atoi return

                              vippstar@gmail. com wrote:
                              ....
                              gets(stdout); is safe I believe, just like atoi("123"); is safe.
                              You might want to rethink that assertion - recheck the interface for gets().

                              The function that allows you to specify the file handle is fgets(), not
                              gets(), and it is possible to use it safely, because it also takes an
                              length argument for the buffer. However, using it on stdout seems, at
                              best, problematic, since stdout is defined as an output stream.

                              I suspect that you mean something substantially different from what you
                              wrote.

                              Comment

                              Working...