473,383 Members | 1,861 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

how can i Know whether atoi function call succeed?

Hi all,
since the function atof, atoi, _atoi64, atol returned value are

Return Values

Each function returns the double, int, __int64 or long value produced
by interpreting the input characters as a number. The return value is 0
(for atoi and _atoi64), 0L (for atol), or 0.0 (for atof) if the input
cannot be converted to a value of that type. The return value is
undefined in case of overflow.

how can I tell if these function calls fail or succeed?
thanks.

Baumann@Pan

Nov 15 '05 #1
15 5102
Anonymousgoogledeja wrote:
Hi all,
since the function atof, atoi, _atoi64, atol returned value are

Return Values

Each function returns the double, int, __int64 or long value produced
by interpreting the input characters as a number. The return value is 0
(for atoi and _atoi64), 0L (for atol), or 0.0 (for atof) if the input
cannot be converted to a value of that type. The return value is
undefined in case of overflow.

how can I tell if these function calls fail or succeed?


_atoi64 is not a standard function but for the rest, you cannot
portably tell if they succeed or fail. Some systems may set errno on
failure but this is not required. This is a good reason to use strto*
functions instead.

Robert Gamble

Nov 15 '05 #2
On 21 Aug 2005 19:55:47 -0700, "Anonymousgoogledeja"
<as*******@hotmail.com> wrote in comp.lang.c:

Amazing, you actually printed part of the documentation yet you missed
the most important point!
Hi all,
since the function atof, atoi, _atoi64, atol returned value are

Return Values

Each function returns the double, int, __int64 or long value produced
by interpreting the input characters as a number. The return value is 0
(for atoi and _atoi64), 0L (for atol), or 0.0 (for atof) if the input
cannot be converted to a value of that type. The return value is ^^^^^^^^^^^^^^^^^^^ undefined in case of overflow. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ how can I tell if these function calls fail or succeed?


Actually the text you quoted it not quite correct. If you call one of
the ato... functions and the converted value is outside the range of
the destination type, the behavior is undefined, not just the return
value. That means, according to the C standard, any of the usual and
nasty symptoms of undefined behavior can happen: reformatting your
hard drive, causing demons to fly out of your nose, causing your
operating system to display a sarcastic message to the user and
terminate your program... The list of possibilities is endless.

The best way to use the ato... functions is to completely omit them
from your program. Unless you pre-check the string to verify that the
result will be in range, in which case you might as well do the
conversion at the same time.

Instead, use the much safer strto... functions prototyped <stdlib.h>
which have defined behavior no matter what the input looks like, so
long as it is not a null pointer. If you pass a valid second pointer
value, you can even tell if a result of 0 is caused by an input of
"0", or by not having anything to convert.

Look at the sample code here:

http://www.jk-technology.com/c/code/strtol.html

--
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.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 15 '05 #3

Robert Gamble wrote:
Anonymousgoogledeja wrote:
Hi all,
since the function atof, atoi, _atoi64, atol returned value are

Return Values

Each function returns the double, int, __int64 or long value produced
by interpreting the input characters as a number. The return value is 0
(for atoi and _atoi64), 0L (for atol), or 0.0 (for atof) if the input
cannot be converted to a value of that type. The return value is
undefined in case of overflow.

how can I tell if these function calls fail or succeed?


_atoi64 is not a standard function but for the rest, you cannot
portably tell if they succeed or fail. Some systems may set errno on
failure but this is not required. This is a good reason to use strto*
functions instead.

Robert Gamble

what's underflow? i can figure out what overflow is.

what about the return value of strtoul('0',0,10)? how can i know if the
returned value 0 is the converted or because there is an error while
converting.

thanks

Nov 15 '05 #4
Jack Klein wrote:
<snip>
...
Look at the sample code here:

http://www.jk-technology.com/c/code/strtol.html
[From the page...]
for ( ; ; )
{
printf("Enter an int, return only to quit: ");
fflush(stdout);
fgets(buff, sizeof buff, stdin);
You should check the return value of fgets() here.
if (buff [0] == '\n')
{
break;
} <snip> /* here is where you might want to add code to */
/* test for extra non-numeric characters at the */
/* end of the input string, if you want that to */
/* be an error */
/* */
/* if you want to generate this error, remove */
/* the two preprocessor lines that start with */
/* #if 0 and #endif */

#if 0
If I change this to 1, the code below will issue a misleading message
(from the point of view of a typical end user) since fgets() generally
leaves a trailing new-line.
else if ('\0' != *end_ptr)
{
printf("extra characters on input line\n");
}
#endif


--
Peter

Nov 15 '05 #5
Anonymousgoogledeja wrote:
Robert Gamble wrote:
Anonymousgoogledeja wrote:
Hi all,
since the function atof, atoi, _atoi64, atol returned value are

Return Values

Each function returns the double, int, __int64 or long value produced
by interpreting the input characters as a number. The return value is 0
(for atoi and _atoi64), 0L (for atol), or 0.0 (for atof) if the input
cannot be converted to a value of that type. The return value is
undefined in case of overflow.

how can I tell if these function calls fail or succeed?
_atoi64 is not a standard function but for the rest, you cannot
portably tell if they succeed or fail. Some systems may set errno on
failure but this is not required. This is a good reason to use strto*
functions instead.


what's underflow? i can figure out what overflow is.


STFW:
<http://en.wikipedia.org/wiki/Underflow>
what about the return value of strtoul('0',0,10)? how can i know if the
returned value 0 is the converted or because there is an error while
converting.


That is the point about reading and trying to understand documentation:
So that you know how to use something (or that you are able to ask
clever questions).

ret = strtoul('0',0,10);
will not even compile.

ret = strtoul("0",0,10);
gives you no chance whatsoever to find out whether you got a zero
or an error. Use this only if you cannot have zero input values.

char *p, *q = "0";
ret = strtoul(q, &p, 10);
gives you the chance to check for errors on zero:
if (ret == 0 && p == q) {
/* error */
}
Moreover, you can check for overflow
else if (ret == ULONG_MAX && errno == ERANGE) {
/* error */
}
This means that you have to #include <errno.h>, of course.
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Nov 15 '05 #6
Anonymousgoogledeja wrote:
Robert Gamble wrote:
Anonymousgoogledeja wrote:
Hi all,
since the function atof, atoi, _atoi64, atol returned value are

Return Values

Each function returns the double, int, __int64 or long value produced
by interpreting the input characters as a number. The return value is 0
(for atoi and _atoi64), 0L (for atol), or 0.0 (for atof) if the input
cannot be converted to a value of that type. The return value is
undefined in case of overflow.

how can I tell if these function calls fail or succeed?
_atoi64 is not a standard function but for the rest, you cannot
portably tell if they succeed or fail. Some systems may set errno on
failure but this is not required. This is a good reason to use strto*
functions instead.

Robert Gamble

what's underflow? i can figure out what overflow is.


Underflow occurs when the result is too small to be stored in the
designated type (FLT_MIN/2 for a float for instance), overflow occurs
when the result is too large to be stored in the designated type (for
example INT_MAX+1 for an int).
what about the return value of strtoul('0',0,10)? how can i know if the
returned value 0 is the converted or because there is an error while
converting.


The first argument to strtoul must be a pointer to char, '0' is an
integer (character constants are integers in C), you must mean
strtoul("0", 0, 10). If strtoul returns 0 you know that either the
result of the conversion was 0 or that no conversion could be
performed. In this particular example you cannot portably tell the
difference, some implementations set errno to EINVAL in this case but
this is not required, the Standard doesn't even define EINVAL. If you
had provided a valid non-null value as the second argument to strtoul
you would be able to recognize the lack of conversion as the value
stored in the pointer whose address you provided would be set to the
value of the first argument.

For example:

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

int main (void) {
unsigned long ul;
char * nptr = "invalid";
char * endptr;
ul = strtoul(nptr, &endptr, 10);
if (ul == 0 && nptr == endptr) {
printf("No conversion occured\n");
}
return 0;
}

Note that if nptr==endptr then ul will always be 0 so the first part of
the test is superfluous.

The documentation that came with your implementation should cover all
of this, please refer to it before asking your next question.

Robert Gamble

Nov 15 '05 #7
Robert Gamble wrote:
Anonymousgoogledeja wrote:
Robert Gamble wrote:
Anonymousgoogledeja wrote:

Hi all,
since the function atof, atoi, _atoi64, atol returned value are

Return Values

Each function returns the double, int, __int64 or long value produced
by interpreting the input characters as a number. The return value is 0
(for atoi and _atoi64), 0L (for atol), or 0.0 (for atof) if the input
cannot be converted to a value of that type. The return value is
undefined in case of overflow.

how can I tell if these function calls fail or succeed?

_atoi64 is not a standard function but for the rest, you cannot
portably tell if they succeed or fail. Some systems may set errno on
failure but this is not required. This is a good reason to use strto*
functions instead.

Robert Gamble

what's underflow? i can figure out what overflow is.

Underflow occurs when the result is too small to be stored in the
designated type (FLT_MIN/2 for a float for instance), overflow occurs

^^^^^^^^^
FLT_MIN is the minimum normalized float >0. You can have every number
down to FLT_MIN*FLT_EPSILON, so a better example is
FLT_MIN*FLT_EPSILON/2
when the result is too large to be stored in the designated type (for
example INT_MAX+1 for an int).


<rest snipped>

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Nov 15 '05 #8

Robert Gamble wrote:
Anonymousgoogledeja wrote:
Robert Gamble wrote:
Anonymousgoogledeja wrote:
> Hi all,
>
>
> since the function atof, atoi, _atoi64, atol returned value are
>
> Return Values
>
> Each function returns the double, int, __int64 or long value produced
> by interpreting the input characters as a number. The return value is 0
> (for atoi and _atoi64), 0L (for atol), or 0.0 (for atof) if the input
> cannot be converted to a value of that type. The return value is
> undefined in case of overflow.
>
> how can I tell if these function calls fail or succeed?

_atoi64 is not a standard function but for the rest, you cannot
portably tell if they succeed or fail. Some systems may set errno on
failure but this is not required. This is a good reason to use strto*
functions instead.

Robert Gamble

what's underflow? i can figure out what overflow is.


Underflow occurs when the result is too small to be stored in the
designated type (FLT_MIN/2 for a float for instance), overflow occurs
when the result is too large to be stored in the designated type (for
example INT_MAX+1 for an int).
what about the return value of strtoul('0',0,10)? how can i know if the
returned value 0 is the converted or because there is an error while
converting.


The first argument to strtoul must be a pointer to char, '0' is an
integer (character constants are integers in C), you must mean
strtoul("0", 0, 10). If strtoul returns 0 you know that either the
result of the conversion was 0 or that no conversion could be
performed. In this particular example you cannot portably tell the
difference, some implementations set errno to EINVAL in this case but
this is not required, the Standard doesn't even define EINVAL. If you
had provided a valid non-null value as the second argument to strtoul
you would be able to recognize the lack of conversion as the value
stored in the pointer whose address you provided would be set to the
value of the first argument.

For example:

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

int main (void) {
unsigned long ul;
char * nptr = "invalid";
char * endptr;
ul = strtoul(nptr, &endptr, 10);
if (ul == 0 && nptr == endptr) {
printf("No conversion occured\n");
}
return 0;
}

Note that if nptr==endptr then ul will always be 0 so the first part of
the test is superfluous.

The documentation that came with your implementation should cover all
of this, please refer to it before asking your next question.

Robert Gamble

thanks, compare the endptr & nptr is the good way to know more about
the returned value 0 .

Nov 15 '05 #9
Michael Mair wrote:
Robert Gamble wrote:
Anonymousgoogledeja wrote:
.... snip ...

what's underflow? i can figure out what overflow is.


Underflow occurs when the result is too small to be stored in the
designated type (FLT_MIN/2 for a float for instance), overflow occurs

^^^^^^^^^
FLT_MIN is the minimum normalized float >0. You can have every number
down to FLT_MIN*FLT_EPSILON, so a better example is
FLT_MIN*FLT_EPSILON/2


Only if the real arithmetic system implements gradual underflow,
which is not mandated by the C standard, although it is by the IEEE
float standard.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson

Nov 15 '05 #10
On 21 Aug 2005 19:55:47 -0700, "Anonymousgoogledeja"
<as*******@hotmail.com> wrote:
Hi all,
since the function atof, atoi, _atoi64, atol returned value are

Return Values

Each function returns the double, int, __int64 or long value produced
by interpreting the input characters as a number. The return value is 0
(for atoi and _atoi64), 0L (for atol), or 0.0 (for atof) if the input
cannot be converted to a value of that type. The return value is
undefined in case of overflow.

how can I tell if these function calls fail or succeed?

You can't. Look at the strtol function instead. It returns a pointer
to the first character not converted, and sets errno if no conversion
can be performed.
--
Al Balmer
Balmer Consulting
re************************@att.net
Nov 15 '05 #11
Michael Mair wrote:
Robert Gamble wrote:
Anonymousgoogledeja wrote:
Robert Gamble wrote:

Anonymousgoogledeja wrote:

>Hi all,
>
>
>since the function atof, atoi, _atoi64, atol returned value are
>
>Return Values
>
>Each function returns the double, int, __int64 or long value produced
>by interpreting the input characters as a number. The return value is 0
>(for atoi and _atoi64), 0L (for atol), or 0.0 (for atof) if the input
>cannot be converted to a value of that type. The return value is
>undefined in case of overflow.
>
>how can I tell if these function calls fail or succeed?

_atoi64 is not a standard function but for the rest, you cannot
portably tell if they succeed or fail. Some systems may set errno on
failure but this is not required. This is a good reason to use strto*
functions instead.

Robert Gamble
what's underflow? i can figure out what overflow is.

Underflow occurs when the result is too small to be stored in the
designated type (FLT_MIN/2 for a float for instance), overflow occurs

^^^^^^^^^
FLT_MIN is the minimum normalized float >0. You can have every number
down to FLT_MIN*FLT_EPSILON, so a better example is
FLT_MIN*FLT_EPSILON/2


As Chuck pointed out, this only applies to systems that support gradual
underflow which is not required by the Standard. Many systems do
support this though and it wouldn't have hurt to preface my statement
with "on implementations that do not support subnormal floating-point
numbers...", thanks for the clarification.

Robert Gamble

Nov 15 '05 #12

Michael Mair wrote:
char *p, *q = "0";
ret = strtoul(q, &p, 10);
gives you the chance to check for errors on zero:
if (ret == 0 && p == q) {
/* error */
}
Moreover, you can check for overflow
else if (ret == ULONG_MAX && errno == ERANGE) {
/* error */
}
This means that you have to #include <errno.h>, of course.


You also need to initialize errno to 0 (or at least not ERANGE) in
order to tell if it was this call to strtoul that failed.

Nov 15 '05 #13
tedu wrote:
Michael Mair wrote:
char *p, *q = "0";
ret = strtoul(q, &p, 10);
gives you the chance to check for errors on zero:
if (ret == 0 && p == q) {
/* error */
}
Moreover, you can check for overflow
else if (ret == ULONG_MAX && errno == ERANGE) {
/* error */
}
This means that you have to #include <errno.h>, of course.


You also need to initialize errno to 0 (or at least not ERANGE) in
order to tell if it was this call to strtoul that failed.


Thanks
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Nov 15 '05 #14
CBFalconer wrote:
Michael Mair wrote:
Robert Gamble wrote:
Anonymousgoogledeja wrote:

... snip ...
what's underflow? i can figure out what overflow is.

Underflow occurs when the result is too small to be stored in the
designated type (FLT_MIN/2 for a float for instance), overflow occurs


^^^^^^^^^
FLT_MIN is the minimum normalized float >0. You can have every number
down to FLT_MIN*FLT_EPSILON, so a better example is
FLT_MIN*FLT_EPSILON/2


Only if the real arithmetic system implements gradual underflow,
which is not mandated by the C standard, although it is by the IEEE
float standard.


True. Thanks for the enlightenment :-)
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Nov 15 '05 #15
On 21 Aug 2005 22:39:04 -0700, "Peter Nilsson" <ai***@acay.com.au>
wrote in comp.lang.c:
Jack Klein wrote:
<snip>
...
Look at the sample code here:

http://www.jk-technology.com/c/code/strtol.html


[From the page...]
for ( ; ; )
{
printf("Enter an int, return only to quit: ");
fflush(stdout);
fgets(buff, sizeof buff, stdin);


You should check the return value of fgets() here.
if (buff [0] == '\n')
{
break;
}

<snip>
/* here is where you might want to add code to */
/* test for extra non-numeric characters at the */
/* end of the input string, if you want that to */
/* be an error */
/* */
/* if you want to generate this error, remove */
/* the two preprocessor lines that start with */
/* #if 0 and #endif */

#if 0


If I change this to 1, the code below will issue a misleading message
(from the point of view of a typical end user) since fgets() generally
leaves a trailing new-line.
else if ('\0' != *end_ptr)
{
printf("extra characters on input line\n");
}
#endif


Thanks pointing out these issues. I have updated the page.

--
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.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 15 '05 #16

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

Similar topics

3
by: Robbie | last post by:
Someone tell me, if not exist, how to write one with the same function? I have made a try, but there is no character type in python, so i failed, someone help me, thanks very much
19
by: Mike Moum | last post by:
I think there may be a bug in string.atoi and string.atol. Here's some output from idle. > Python 2.3.4 (#2, Jan 5 2005, 08:24:51) > on linux2 > Type "copyright", "credits" or "license()"...
6
by: John Smith | last post by:
What's wrong with the use of atoi in the following code? Why do I get the error message: 'atoi' : cannot convert parameter 1 from 'char' to 'const char *' char cBuffer; void...
14
by: Kayle | last post by:
How should we check if the '\0' characters exists in the string as I am confused that some books mentioned that we have to check whether we need to make sure that we pass the...
5
by: Bansidhar | last post by:
atoi() function seems not to have any support for Hex, octal number. Usually when I read from a text file then it contain number like 0x232 etc. In this case atoi() fells. In case of itoa() there...
47
by: sudharsan | last post by:
Hi could you please explain wat atoi( ) function is for and an example how to use it?
13
by: ptq2238 | last post by:
Hi, I have written this code to help me learn C but I'm not sure why gcc - Wall is giving me an error when I compile Basically I want to read in a character then a number and then manipulate...
3
by: pauldepstein | last post by:
The following description of atoi is pasted from cplusplus.com. My question is after the pasting. ***** PASTING BEGINS HERE ****** int atoi ( const char * str ); <cstdlib> Convert string...
4
by: Ram | last post by:
Hi All, Firstly i am a newbie and trying to learn C. The background of the problem is Program: Presently I am working on a program of numerology and the I/P will be the name and output...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.