473,473 Members | 1,848 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

what should atoi() do with garbage input?

Can someone tell me (hopefully with a pointer to the appropriate place
in the Standard), what atoi() ought to do with the following:
int outp = 0;

outp = atoi("2+*&^%$#@!");
One school of thought on my team is that atoi() is supposed to
"grab the '2', convert it to 2, and basically just toss all that
'+*&^%$#@!' stuff." Another school of thought is that atoi() should
fail because the entire input was not convertiable to an int. Thanks
in advance for any assistance.
Nov 13 '05 #1
6 4251
In article <94**************************@posting.google.com >, Steve Gallagher wrote:
Can someone tell me (hopefully with a pointer to the appropriate place
in the Standard), what atoi() ought to do with the following:
int outp = 0;

outp = atoi("2+*&^%$#@!");
One school of thought on my team is that atoi() is supposed to
"grab the '2', convert it to 2, and basically just toss all that
'+*&^%$#@!' stuff." Another school of thought is that atoi() should
fail because the entire input was not convertiable to an int. Thanks
in advance for any assistance.


atoi() will grab the '2' but ignore the rest, just like strtol()
would do. It ignores any leading whitespace characters in the
strings, looks at what the standard calls "the subject sequence"
(everything that looks like a possibly signed floating point
number, or hex or one of NAN or INFINITY etc.) and converts it,
and ignores everything else. The current locale playes a role
as well.

It's in section 7.20.1.3.

--
Andreas Kähäri
Nov 13 '05 #2
"Steve Gallagher" <je*********@hotmail.com> wrote:
Can someone tell me (hopefully with a pointer to the
appropriate place in the Standard), what atoi() ought
to do with the following:
int outp = 0;

outp = atoi("2+*&^%$#@!");
One school of thought on my team is that atoi() is
supposed to "grab the '2', convert it to 2, and basically
just toss all that '+*&^%$#@!' stuff." Another school of
thought is that atoi() should fail because the entire
input was not convertiable to an int. Thanks in advance
for any assistance.


C99 7.20.1.2#2
Except for the behavior on error, they are equivalent
to
atoi: (int)strtol(nptr, (char **)NULL, 10)

C99 7.20.1.4#2
First, they decompose the input string into three
parts: an initial, possibly empty, sequence of
white-space characters (as specified by the isspace
function), a subject sequence resembling an integer
represented in some radix determined by the value of
base, and a final string of one or more unrecognized
characters, including the terminating null character of
the input string. Then, they attempt to convert the
subject sequence to an integer, and return the result.

So, it should separate the input string "2+*&^%$#@!" into:
initial: ""
subject: "2"
final: "+*&^%$#@!"
It should convert the subject, and ignore the rest of the
string.

--
Simon.
Nov 13 '05 #3
"Simon Biber" <ne**@ralminNOSPAM.cc> wrote:
"Steve Gallagher" <je*********@hotmail.com> wrote:
Can someone tell me (hopefully with a pointer to the
appropriate place in the Standard), what atoi() ought
to do with the following:

int outp = 0;

outp = atoi("2+*&^%$#@!");
C99 7.20.1.2#2
Except for the behavior on error, they are equivalent
to
atoi: (int)strtol(nptr, (char **)NULL, 10)


Also note that, because of the better behaviour on error referred to in
this clause, strtol() is superior to atoi(). You'd be better off not
using atoi() at all, but switching to strtol().

Richard
Nov 13 '05 #4
In <sl**********************@mx.freeshell.org> Andreas Kahari <ak*******@freeshell.org> writes:
In article <94**************************@posting.google.com >, Steve Gallagher wrote:
Can someone tell me (hopefully with a pointer to the appropriate place
in the Standard), what atoi() ought to do with the following:
int outp = 0;

outp = atoi("2+*&^%$#@!");
One school of thought on my team is that atoi() is supposed to
"grab the '2', convert it to 2, and basically just toss all that
'+*&^%$#@!' stuff." Another school of thought is that atoi() should
fail because the entire input was not convertiable to an int. Thanks
in advance for any assistance.
atoi() will grab the '2' but ignore the rest, just like strtol()
would do. It ignores any leading whitespace characters in the
strings, looks at what the standard calls "the subject sequence"
(everything that looks like a possibly signed floating point

^^^^^^^^^^^^^^number, or hex or one of NAN or INFINITY etc.) and converts it, ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^and ignores everything else. The current locale playes a role
as well.


Are you *sure* you know what you're talking about?

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #5
In article <bo**********@sunnews.cern.ch>, Dan Pop wrote:
In <sl**********************@mx.freeshell.org> Andreas Kahari
<ak*******@freeshell.org> writes:

[cut]
(everything that looks like a possibly signed floating point

^^^^^^^^^^^^^^
number, or hex or one of NAN or INFINITY etc.) and converts it,

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
and ignores everything else. The current locale playes a role
as well.


Are you *sure* you know what you're talking about?


I thought so, but I'm getting the feeling I'm now wrong. It's
only strtod() that knows about NAN and INFINITY, isn't it? I
haven't got the standard here at the moment.

Would a strtol() implementation that converts strings like
"1.2e3" into floating point numbers and then casts them to
integers ("1.2e3" --> 1200) violate the standard?

--
Andreas Kähäri
Nov 13 '05 #6
In <sl*********************@mx.freeshell.org> Andreas Kahari <ak*******@freeshell.org> writes:
In article <bo**********@sunnews.cern.ch>, Dan Pop wrote:
In <sl**********************@mx.freeshell.org> Andreas Kahari
<ak*******@freeshell.org> writes:[cut]
(everything that looks like a possibly signed floating point

^^^^^^^^^^^^^^
number, or hex or one of NAN or INFINITY etc.) and converts it,

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
and ignores everything else. The current locale playes a role
as well.


Are you *sure* you know what you're talking about?


I thought so, but I'm getting the feeling I'm now wrong. It's
only strtod() that knows about NAN and INFINITY, isn't it? I
haven't got the standard here at the moment.


Yup, strtod and its C99 friends, strtof and strtold.
Would a strtol() implementation that converts strings like
"1.2e3" into floating point numbers and then casts them to
integers ("1.2e3" --> 1200) violate the standard?


Most definitely. The right value (modulo locale-specific oddities) is
1 for "1.2e3", because the dot/decimal point is not part of the
subject sequence for an integer constant.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: ratlhead | last post by:
Hey all, I'm attempting to do some form processing on a server that has register_globals off, however, I've run into a confusing situation and need some help. Basically, the form is a...
23
by: ern | last post by:
I have a program that runs scripts. If the user types "script myScript.dat" the program will grab commands from the text file, verify correctness, and begin executing the script UNTIL... I need...
1
by: Gary Nastrasio | last post by:
Is it possible to use "normal" C types with garbage collection? Here's the function I'd like to write, but not have to worry about deallocation: char* ConvertStringToChar(String* pString) {...
7
by: Drew Berkemeyer | last post by:
I've encounted a pretty strange problem and I'm not quite sure what to make of it. I have a web service that consumes an XML file as well as a few other parameters. This web service works fine...
2
by: Plissken.s | last post by:
Hi, Is it possible to over load a function of a class with const input parameter ? class A { public: void aFunc(const B& b); void aFunc(B& b); }
2
by: vinkumar | last post by:
Hi, I have to search for string and replace with user input value using js. I have used inner HTML (objExplorer.Document.body.innerHTML) to get user input. (Eg: If user inputs web port no. as...
1
by: kingshuk | last post by:
OS : WinXp ORACLE VERSION : Oracle 10g I m unable to use the loop in PL/SQL with user input. When ever i am going to use the block of code is shows error...... /* TABLE WITH DATA TYPE */ ...
3
by: Hodge4ever | last post by:
Just to give you some background, I am trying to write an encryption algorithmn, so the options are -e to encrypt or -d to decrypt. During the encryption process an array of characters is written to...
5
leannsmarie
by: leannsmarie | last post by:
Hi Everyone, I have a problem with a program I have been assigned thats driving me crazy and I hope someone with a fresh eye could point me in the right direction. Im using Visual Basic Express...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.