atoi error

R

Rodion

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

i=2617245696
 
S

Shanmuhanathan T

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

i=2617245696


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

jacob navia

Using lcc-win32 compiler I compiled this
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
char * str = "10974000000";
long long i = strtoll(str,NULL,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.


http://www.cs.virginia.edu/~lcc-win32
 
J

Jens.Toerring

Rodion said:
How can I get correct result:
char * str;
str = "10974000000";
i = atoi(str);
printf("i=" , i);

That would be

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

or something similar, wouldn't it?
----- output:

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
 
R

Richard Tobin

Rodion said:
str = "10974000000";
i = atoi(str);

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

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

-- Richard
 
T

Thomas Matthews

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

i=2617245696
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:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
 
A

Al Bowers

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

i=2617245696

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,10);
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 = "10974000000";

printf("\"%s\" can%s be represented as type int\n",
str,validINT(str, NULL)?"":"not");
return 0;
}
 
J

Jack Klein

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

i=2617245696

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

bowsayge

Rodion said to us:
How can I get correct result:
char * str;
str = "10974000000";
i = atoi(str);
printf("i=" , i);
----- output:

i=2617245696

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 = "10974000000";
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.
 
M

Mark McIntyre

How can I get correct result:

Firstly, by writing actual code that compiles and is valid C..
i = atoi(str);
printf("i=" , i);

both the above lines are meaningless. Try actually posting your code, not
some abberviation of it which you badly remembered....
If str is smaller(10974000) then result is correct

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

Daniel Haude

On 20 Aug 2004 14:03:28 GMT,
in Msg. said:
int (probably 32 bits). But the answer you give:


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

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

--Daniel
 
R

Richard Tobin

int (probably 32 bits). But the answer you give:


is not what I would expect, so perhaps you have some other problem
too.
[/QUOTE]
What exactly do you expect in a case of undefined behavior?

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
 
D

Dan Pop

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

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?[/QUOTE]

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
 
R

Richard Tobin

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.

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
 
D

Dan Pop

In said:
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.

How do you know overflow actually did happen?
The fact that nature of undefined behaviour is often quite predictable
is of great value in debugging.

Indeed, if you have access to the actual code and know the exact effect
of that particular instance of undefined behaviour on that particular
platform. Which is not the case here. Without seeing the actual atoi
code, there is no way to predict the result of the code in question.

Dan
 
D

Dave Thompson

Rodion said to us:
atoi [of] "10974000000";
Forgive Bowsayge because this is not ANSI-C, but if you have
a compiler with the "long long" datatype (e.g. gcc), this works:
long long >= 64 bit is standard as of C99; which actually is, as C90
was, an ISO standard, adopted by ANSI (but only in 2000). And you need
library support as well, especially true of gcc since it can be used
with multiple libraries whereas many other compilers are tied to one.
char * str = "10974000000";
long long i = 0;
if (sscanf(str, "%lld", & i) > 0) {
printf("i=%lld\n" , i);
}

Can also use i = atoll (str) or better strtoll (str, NULL, 10).

A correct C99 implementation has all three, but lack of the library
routines is usually diagnosed by link time, while nonsupport of the
sscanf modifier may not be detected until runtime or even at all.
You have a lot of insignificant zeros in that number. Perhaps
floating-point variables will suit you.

Why do you think they're insignificant? The OP didn't say anything
about error bars or uncertainty. All we know is they're trailing.

- David.Thompson1 at worldnet.att.net
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,146
Messages
2,570,832
Members
47,374
Latest member
anuragag27

Latest Threads

Top