atoi error

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

    atoi error

    How can I get correct result:
    char * str;
    str = "1097400000 0";
    i = atoi(str);
    printf("i=" , i);
    ----- output:

    i=2617245696

    ---
    If str is smaller(1097400 0) then result is correct
  • Tom St Denis

    #2
    Re: atoi error

    Rodion wrote:
    [color=blue]
    > How can I get correct result:
    > char * str;
    > str = "1097400000 0";[/color]

    This is a 34 bit number.
    [color=blue]
    > i = atoi(str);[/color]

    atoi presumably returns 32 bits on your platform.

    [i'd use sscanf myself..]

    Tom

    Comment

    • Shanmuhanathan T

      #3
      Re: atoi error

      on 8/20/2004 6:40 PM Rodion Wrote:[color=blue]
      > How can I get correct result:
      > char * str;
      > str = "1097400000 0";
      > i = atoi(str);
      > printf("i=" , i);
      > ----- output:
      >
      > i=2617245696
      >
      > ---
      > If str is smaller(1097400 0) then result is correct[/color]


      you havenot declared i.
      Lookup the syntax of printf() in a good
      c reference book.
      I am skipping a lot of the usual tirade about
      undefined behaviour.

      If you go thorough most of the posts which
      got useful answers, you will notice that most
      of them have the smallest piece of compilable
      code which illustrates the problem at hand.
      In this case, it might even solve your problem.

      --
      Shanmu.

      Comment

      • Jens.Toerring@physik.fu-berlin.de

        #4
        Re: atoi error

        Rodion <m_rodion@mail. ru> wrote:[color=blue]
        > How can I get correct result:
        > char * str;
        > str = "1097400000 0";
        > i = atoi(str);
        > printf("i=" , i);[/color]

        That would be

        printf("i=%d\n" , i);

        or something similar, wouldn't it?
        [color=blue]
        > ----- output:[/color]
        [color=blue]
        > i=2617245696[/color]
        [color=blue]
        > ---
        > If str is smaller(1097400 0) then result is correct[/color]

        As someone else already pointed out on your system the number is
        probably too large to be represented by an int. I would strongly
        recommend that you give up on atoi() and instead use strtol().
        Not only will it let you convert that number to a long, it also
        gives you a chance to do serious error checking by e.g. setting
        errno to ERANGE if the number you passed it could not be stored
        in a long (and it lets you find out how much of the input string
        was converted etc.).
        Regards, Jens
        --
        \ Jens Thoms Toerring ___ Jens.Toerring@p hysik.fu-berlin.de
        \______________ ____________ http://www.toerring.de

        Comment

        • Richard Tobin

          #5
          Re: atoi error

          In article <415f278d.04082 00510.5d36e491@ posting.google. com>,
          Rodion <m_rodion@mail. ru> wrote:
          [color=blue]
          >str = "1097400000 0";
          >i = atoi(str);[/color]

          The obvious answer is that the number is bigger than the size of an
          int (probably 32 bits). But the answer you give:
          [color=blue]
          >i=2617245696[/color]

          is not what I would expect, so perhaps you have some other problem
          too.

          -- Richard

          Comment

          • Thomas Matthews

            #6
            Re: atoi error

            Rodion wrote:[color=blue]
            > How can I get correct result:
            > char * str;
            > str = "1097400000 0";
            > i = atoi(str);
            > printf("i=" , i);
            > ----- output:
            >
            > i=2617245696
            >
            > ---
            > If str is smaller(1097400 0) then result is correct[/color]
            As others have pointed out, your number is overflowing
            the capacity for a 32-bit int on your system.

            Either try using longs and:
            atol
            strtoul

            Or try a Big Number library (search the web using
            the terms "Big" "Number" "Library").

            One nice advantage of a Big Number is that you
            most likely will not have to worry about overflows
            unless your computer is completely out of memory
            (physical or virtual).

            --
            Thomas Matthews

            C++ newsgroup welcome message:

            C++ Faq: http://www.parashift.com/c++-faq-lite
            C Faq: http://www.eskimo.com/~scs/c-faq/top.html
            alt.comp.lang.l earn.c-c++ faq:

            Other sites:
            http://www.josuttis.com -- C++ STL Library book

            Comment

            • Al Bowers

              #7
              Re: atoi error



              Rodion wrote:[color=blue]
              > How can I get correct result:
              > char * str;
              > str = "1097400000 0";
              > i = atoi(str);
              > printf("i=" , i);
              > ----- output:
              >
              > i=2617245696
              >
              > ---
              > If str is smaller(1097400 0) then result is correct[/color]

              The string is probably too big to be represented as a type
              int. Instead of function atoi you can use function strtol
              robustly to check the validity of the string.

              Example:

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

              typedef enum BOOL{false,true } BOOL;

              BOOL validINT(const char *src, int *result)
              {
              long tmp;
              char *s;

              tmp = strtol(src,&s,1 0);
              if(s == src || *s != '\0' || errno == ERANGE ||
              tmp > INT_MAX || tmp < INT_MIN) return false;
              if(result) *result = (int)tmp;
              return true;
              }

              int main(void)
              {
              char *str = "1097400000 0";

              printf("\"%s\" can%s be represented as type int\n",
              str,validINT(st r, NULL)?"":"not") ;
              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

              • jacob navia

                #8
                Re: atoi error

                Using lcc-win32 compiler I compiled this
                #include <stdlib.h>
                #include <stdio.h>
                int main(void)
                {
                char * str = "1097400000 0";
                long long i = strtoll(str,NUL L,10);
                printf("i=%lld" , i);
                }
                Output
                i=10974000000

                1: the number 10974000000 will not fit into a 32 bit int.
                Hence I declared it long long (64 bits).
                2: atoi can't be used since it returns an int, not a long long.
                Use strtoll for that (string to long long)
                3: When using printf you must tell printf which format you
                want for the data. You can't put
                printf("i=",i);
                You should write:
                printf("i=%d\n" ,i);
                or, in this concrete example
                printf("i=%lld\ n",i);
                The instruction "%lld" tells the printf function what kind of
                data it should expect.





                Comment

                • Jack Klein

                  #9
                  Re: atoi error

                  On 20 Aug 2004 06:10:40 -0700, m_rodion@mail.r u (Rodion) wrote in
                  comp.lang.c:
                  [color=blue]
                  > How can I get correct result:
                  > char * str;
                  > str = "1097400000 0";
                  > i = atoi(str);
                  > printf("i=" , i);
                  > ----- output:
                  >
                  > i=2617245696
                  >
                  > ---
                  > If str is smaller(1097400 0) then result is correct[/color]

                  The function atoi() attempts to convert a text string representing a
                  numeric value into a signed int. If the result of the conversion is
                  outside the range of values that a signed int can hold, the behavior
                  is undefined.

                  What is the range of type signed int on your platform? You can find
                  the macros INT_MIN and INT_MAX in the <limits.h> header. Most likely,
                  the largest value that your int can hold is less than 10974000000, so
                  you are trying to pour 10 quarts of water into a 5 quart pitcher.

                  Note that the strto* functions (strtol, strtoul, strtod) prototyped in
                  <stdlib.h> are much better and safer than the ato* functions. They
                  will not even try to put a value that won't fit into a result, and
                  they provide indications in errno if there is a problem.

                  --
                  Jack Klein
                  Home: http://JK-Technology.Com
                  FAQs for
                  comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
                  comp.lang.c++ http://www.parashift.com/c++-faq-lite/
                  alt.comp.lang.l earn.c-c++

                  Comment

                  • bowsayge

                    #10
                    Re: atoi error

                    Rodion said to us:
                    [color=blue]
                    > How can I get correct result:
                    > char * str;
                    > str = "1097400000 0";
                    > i = atoi(str);
                    > printf("i=" , i);
                    > ----- output:
                    >
                    > i=2617245696
                    >
                    > ---
                    > If str is smaller(1097400 0) then result is correct[/color]

                    Forgive Bowsayge because this is not ANSI-C, but if you have
                    a compiler with the "long long" datatype (e.g. gcc), this works:

                    char * str = "1097400000 0";
                    long long i = 0;
                    if (sscanf(str, "%lld", & i) > 0) {
                    printf("i=%lld\ n" , i);
                    }

                    You have a lot of insignificant zeros in that number. Perhaps
                    floating-point variables will suit you.

                    Comment

                    • Mark McIntyre

                      #11
                      Re: atoi error

                      On 20 Aug 2004 06:10:40 -0700, in comp.lang.c , m_rodion@mail.r u (Rodion)
                      wrote:
                      [color=blue]
                      >How can I get correct result:[/color]

                      Firstly, by writing actual code that compiles and is valid C..
                      [color=blue]
                      >i = atoi(str);
                      >printf("i=" , i);[/color]

                      both the above lines are meaningless. Try actually posting your code, not
                      some abberviation of it which you badly remembered....
                      [color=blue]
                      >If str is smaller(1097400 0) then result is correct[/color]

                      Thats because you need to be able to fit it into whatever type i is (you
                      neglected to mention what). To get larger numbers in, use a wider type.


                      --
                      Mark McIntyre
                      CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
                      CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt >


                      ----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
                      http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
                      ---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---

                      Comment

                      • Daniel Haude

                        #12
                        Re: atoi error

                        On 20 Aug 2004 14:03:28 GMT,
                        Richard Tobin <richard@cogsci .ed.ac.uk> wrote
                        in Msg. <cg50bg$ugh$1@p c-news.cogsci.ed. ac.uk>
                        [color=blue]
                        > int (probably 32 bits). But the answer you give:
                        >[color=green]
                        >>i=261724569 6[/color]
                        >
                        > is not what I would expect, so perhaps you have some other problem
                        > too.[/color]

                        What exactly do you expect in a case of undefined behavior?

                        --Daniel

                        --
                        "With me is nothing wrong! And with you?" (from r.a.m.p)

                        Comment

                        • Richard Tobin

                          #13
                          Re: atoi error

                          In article <slrncijq2j.10t .haude@kir.phys net.uni-hamburg.de>,
                          Daniel Haude <haude@physnet. uni-hamburg.de> wrote:
                          [color=blue][color=green]
                          >> int (probably 32 bits). But the answer you give:
                          >>[color=darkred]
                          >>>i=26172456 96[/color]
                          >>
                          >> is not what I would expect, so perhaps you have some other problem
                          >> too.[/color][/color]
                          [color=blue]
                          >What exactly do you expect in a case of undefined behavior?[/color]

                          In this case, I expect the atoi function to use an algorithm that
                          works for numbers that fit in 32 bits. Can you think of such an
                          algorithm that produces the above value for the input given? Since
                          the OP said he got that value, I'm thinking about realistic
                          implementations , not ones constructed to demonstrate the possibilities
                          of undefined behaviour.

                          -- Richard

                          Comment

                          • Dan Pop

                            #14
                            Re: atoi error

                            In <cgcqjt$dk0$2@p c-news.cogsci.ed. ac.uk> richard@cogsci. ed.ac.uk (Richard Tobin) writes:
                            [color=blue]
                            >In article <slrncijq2j.10t .haude@kir.phys net.uni-hamburg.de>,
                            >Daniel Haude <haude@physnet. uni-hamburg.de> wrote:
                            >[color=green][color=darkred]
                            >>> int (probably 32 bits). But the answer you give:
                            >>>
                            >>>>i=261724569 6
                            >>>
                            >>> is not what I would expect, so perhaps you have some other problem
                            >>> too.[/color][/color]
                            >[color=green]
                            >>What exactly do you expect in a case of undefined behavior?[/color]
                            >
                            >In this case, I expect the atoi function to use an algorithm that
                            >works for numbers that fit in 32 bits. Can you think of such an
                            >algorithm that produces the above value for the input given?[/color]

                            If the implementation of the algorithm invokes undefined behaviour, due
                            to signed arithmetic overflow, the algorithm itself becomes irrelevant.

                            There is little point in speculating about the actual effects of signed
                            arithmetic overflow or about the possible tests the implementor may
                            have used in order to avoid it hapenning.

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

                            Comment

                            • Richard Tobin

                              #15
                              Re: atoi error

                              In article <cgcso4$n4p$1@s unnews.cern.ch> , Dan Pop <Dan.Pop@cern.c h> wrote:
                              [color=blue]
                              >There is little point in speculating about the actual effects of signed
                              >arithmetic overflow or about the possible tests the implementor may
                              >have used in order to avoid it hapenning.[/color]

                              This is, supposedly, someone having problems with a real program. If
                              the result he gets is not one plausibly caused by overflow, then he
                              may have another error, and to ignore it would be unwise.

                              The fact that nature of undefined behaviour is often quite predictable
                              is of great value in debugging.

                              -- Richard

                              Comment

                              Working...