Home > Archive > C > August 2004 > atoi error
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| Rodion 2004-08-26, 3:57 pm |
| How can I get correct result:
char * str;
str = "10974000000";
i = atoi(str);
printf("i=" , i);
----- output:
i=2617245696
---
If str is smaller(10974000) then result is correct
| |
| Mark McIntyre 2004-08-26, 3:57 pm |
| On 20 Aug 2004 06:10:40 -0700, in comp.lang.c , m_rodion@mail.ru (Rodion)
wrote:
>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.
--
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 =---
| |
| Dan Pop 2004-08-26, 3:57 pm |
| In <cgcqjt$dk0$2@pc-news.cogsci.ed.ac.uk> richard@cogsci.ed.ac.uk (Richard Tobin) writes:
>In article <slrncijq2j.10t.haude@kir.physnet.uni-hamburg.de>,
>Daniel Haude <haude@physnet.uni-hamburg.de> wrote:
>
>
>
>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?
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
| |
| Dave Thompson 2004-08-26, 3:57 pm |
| On Sat, 21 Aug 2004 04:06:31 GMT, bowsayge
<bowsayge@nomail.afraid.org> wrote:
> Rodion said to us:
[color=darkred]
> 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
| |
| bowsayge 2004-08-27, 3:55 pm |
| Rodion said to us:
> How can I get correct result:
> char * str;
> str = "10974000000";
> i = atoi(str);
> printf("i=" , i);
> ----- output:
>
> i=2617245696
>
> ---
> If str is smaller(10974000) then result is correct
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.
| |
| Dan Pop 2004-08-30, 3:55 pm |
| In <cgcqjt$dk0$2@pc-news.cogsci.ed.ac.uk> richard@cogsci.ed.ac.uk (Richard Tobin) writes:
>In article <slrncijq2j.10t.haude@kir.physnet.uni-hamburg.de>,
>Daniel Haude <haude@physnet.uni-hamburg.de> wrote:
>
>
>
>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?
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
|
|
|
|
|