473,394 Members | 2,063 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,394 software developers and data experts.

strtol library function

Another two questions...

Is behaviour defined when the first argument of strtol is NULL?

And if the string contains only digits, is the 2nd argument (assuming
it wasn't NULL) guaranteed to be set to a pointer to a pointer to '\0'?
Thanks.

--
David Scarlett

dscarlett@_ _ _ _ _ _ _ _
_ _ _ _ _ optusnet.com.au
Nov 14 '05 #1
16 2637
"David Scarlett" <lo**@my.signature> wrote in message
news:Xn***********************@211.29.133.50...
Another two questions...

Is behaviour defined when the first argument of strtol is NULL?
No.
And if the string contains only digits, is the 2nd argument (assuming
it wasn't NULL) guaranteed to be set to a pointer to a pointer to '\0'?


The second argument is passed by value as a pointer to pointer. It makes
no sense to talke about what it might get set to. But the pointer it
points at on a successful return should designate the terminating NUL,
in the case you describe. I think that's what you really meant.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
Nov 14 '05 #2


David Scarlett wrote:
Another two questions...

Is behaviour defined when the first argument of strtol is NULL?


The first agrument must be a value that represents a pointer to
a string. A value of NULL is not defined for the function.

I have a question regarding errno. Given the following code, which
simply detects if there was a proper coversion or not, is the test
using LONG_MIN or LONG_MAX neccessary or can one use use the test
errno == ERANGE?

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

int main(void)
{
char *str = "-123456789999999999999", *s;
long num = 0;

errno = 0;
num = strtol(str,&s,10);
if(s == str || *s != '\0' ||
((errno == ERANGE) && (num == LONG_MAX || num == LONG_MIN)))
printf("\"%s\" does not convert to a long value\n",str);
else
printf("\"%s\" = %ld\n",str,num);
return 0;
}
--
Al Bowers
Tampa, Fl USA
mailto: xa******@myrapidsys.com (remove the x to send email)
http://www.geocities.com/abowers822/

Nov 14 '05 #3
"Al Bowers" <xa******@rapidsys.com> wrote in message
news:2g************@uni-berlin.de...
I have a question regarding errno. Given the following code, which
simply detects if there was a proper coversion or not, is the test
using LONG_MIN or LONG_MAX neccessary or can one use use the test
errno == ERANGE?

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

int main(void)
{
char *str = "-123456789999999999999", *s;
long num = 0;

errno = 0;
num = strtol(str,&s,10);
if(s == str || *s != '\0' ||
((errno == ERANGE) && (num == LONG_MAX || num == LONG_MIN)))
printf("\"%s\" does not convert to a long value\n",str);
else
printf("\"%s\" = %ld\n",str,num);
return 0;
}


The test of errno should suffice.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
Nov 14 '05 #4


P.J. Plauger wrote:
"Al Bowers" <xa******@rapidsys.com> wrote in message
news:2g************@uni-berlin.de...

I have a question regarding errno. Given the following code, which
simply detects if there was a proper coversion or not, is the test
using LONG_MIN or LONG_MAX neccessary or can one use use the test
errno == ERANGE?

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

int main(void)
{
char *str = "-123456789999999999999", *s;
long num = 0;

errno = 0;
num = strtol(str,&s,10);
if(s == str || *s != '\0' ||
((errno == ERANGE) && (num == LONG_MAX || num == LONG_MIN)))
printf("\"%s\" does not convert to a long value\n",str);
else
printf("\"%s\" = %ld\n",str,num);
return 0;
}

The test of errno should suffice.


Yeah! That is the way I always used it. But recently someone mentioned
that there is nothing to prevent the a function implementation from
setting errno to ERANGE even if the resulting value is in the range
of a long. I looked at the Standard's description of the function and
saw that it id say the an unrepresentable value would result in both
errno == ERANGE and the return value either LONG_MAX or LONG_MIN.

--
Al Bowers
Tampa, Fl USA
mailto: xa******@myrapidsys.com (remove the x to send email)
http://www.geocities.com/abowers822/

Nov 14 '05 #5
In 'comp.lang.c', David Scarlett <lo**@my.signature> wrote:
Is behaviour defined when the first argument of strtol is NULL?
No.
And if the string contains only digits, is the 2nd argument (assuming
it wasn't NULL) guaranteed to be set to a pointer to a pointer to '\0'?


The argument is unchanged. It's the pointed value that is changed to a
pointer to '\0'.

--
-ed- get my email here: http://marreduspam.com/ad672570
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-reference: http://www.dinkumware.com/manuals/reader.aspx?lib=c99
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
Nov 14 '05 #6
On Sat, 15 May 2004 12:51:16 -0400, Al Bowers <xa******@rapidsys.com>
wrote:
Yeah! That is the way I always used it. But recently someone mentioned
that there is nothing to prevent the a function implementation from
setting errno to ERANGE even if the resulting value is in the range
of a long. I looked at the Standard's description of the function and
saw that it id say the an unrepresentable value would result in both
errno == ERANGE and the return value either LONG_MAX or LONG_MIN.


Perhaps that was in the course of my stupid question about strtoul(),
which I erroneously claimed that it returned ERANGE for input between
LONG_MAX and ULONG_MAX. It doesn't. I was actually playing with
strtol() instead, and didn't realize it.

It still seems rather strange to me that strtoul() accepts "-2" and
returns 4294967294 without complaint, but according to the man page it
is supposed to do so.

Nov 14 '05 #7

"Paul Emmons" <pe*****@voicenet.com> a écrit dans le message de
news:dd********************************@4ax.com...
On Sat, 15 May 2004 12:51:16 -0400, Al Bowers <xa******@rapidsys.com>
wrote: [snip]
It still seems rather strange to me that strtoul() accepts "-2" and
returns 4294967294 without complaint, but according to the man page it
is supposed to do so.


But HOW is that function supposed to know that
it was passed -2 and NOT 4294967294 ????

The binary representation of both is exactly the same!

Nov 14 '05 #8
"jacob navia" <ja***@jacob.remcomp.fr> writes:
"Paul Emmons" <pe*****@voicenet.com> a écrit dans le message de
news:dd********************************@4ax.com...
On Sat, 15 May 2004 12:51:16 -0400, Al Bowers <xa******@rapidsys.com>
wrote: [snip]

It still seems rather strange to me that strtoul() accepts "-2" and
returns 4294967294 without complaint, but according to the man page it
is supposed to do so.


But HOW is that function supposed to know that
it was passed -2 and NOT 4294967294 ????


Because the first character of the string passed to it, following
whitespace is '-', or some locale-specific version thereof.
The binary representation of both is exactly the same!


Their text representations differ.

Perhaps you should join Richard Heathfield's "reading for
comprehension" class.
--
Peter Seebach on C99:
"[F]or the most part, features were added, not removed. This sounds
great until you try to carry a full-sized printout of the standard
around for a day."
Nov 14 '05 #9
in comp.lang.c i read:
It still seems rather strange to me that strtoul() accepts "-2" and
returns 4294967294 without complaint, but according to the man page it
is supposed to do so.


for the same reason that assigning -2 to an unsigned long results in that
value.

--
a signature
Nov 14 '05 #10
In <m1*************@usa.net> those who know me have no need of my name <no****************@usa.net> writes:
in comp.lang.c i read:
It still seems rather strange to me that strtoul() accepts "-2" and
returns 4294967294 without complaint, but according to the man page it
is supposed to do so.


for the same reason that assigning -2 to an unsigned long results in that
value.


If it's the same reason, then when should strtoul() set errno to ERANGE
and why?

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #11
In <dd********************************@4ax.com> Paul Emmons <pe*****@voicenet.com> writes:
It still seems rather strange to me that strtoul() accepts "-2" and
returns 4294967294 without complaint, but according to the man page it
is supposed to do so.


According to my reading of the C standard it isn't supposed to do so:

Returns

8 The strtol, strtoll, strtoul, and strtoull functions return
the converted value, if any. If no conversion could be performed,
zero is returned. If the correct value is outside the range of
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
representable values, LONG_MIN, LONG_MAX, LLONG_MIN, LLONG_MAX,
^^^^^^^^^^^^^^^^^^^^
ULONG_MAX, or ULLONG_MAX is returned (according to the return
type and sign of the value, if any), and the value of the macro
ERANGE is stored in errno.

Unless I'm missing something, -2 is outside the range of values that can
be represented by unsigned long. And any argument about "folding" it into
range would equally apply to values above ULONG_MAX, so this function
should NEVER set errno to ERANGE if that argument applied.

However, I know, from past discussions, that committee members disagree
with my interpretation.

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


Dan Pop wrote:
In <dd********************************@4ax.com> Paul Emmons <pe*****@voicenet.com> writes:

It still seems rather strange to me that strtoul() accepts "-2" and
returns 4294967294 without complaint, but according to the man page it
is supposed to do so.

According to my reading of the C standard it isn't supposed to do so:

Returns

8 The strtol, strtoll, strtoul, and strtoull functions return
the converted value, if any. If no conversion could be performed,
zero is returned. If the correct value is outside the range of
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
representable values, LONG_MIN, LONG_MAX, LLONG_MIN, LLONG_MAX,
^^^^^^^^^^^^^^^^^^^^
ULONG_MAX, or ULLONG_MAX is returned (according to the return
type and sign of the value, if any), and the value of the macro
ERANGE is stored in errno.

Unless I'm missing something, -2 is outside the range of values that can
be represented by unsigned long. And any argument about "folding" it into
range would equally apply to values above ULONG_MAX, so this function
should NEVER set errno to ERANGE if that argument applied.


I don't have the previous standard available but I believe the wording
for the description for strtoul was "If the subject sequence field
begins witha a minus sign, the value resulting from the conversion is
negated". Thus the function would first convert "-2" to, for example,
unsigned long x = 2, then it would be negated, x = -x. I can accept that
but I don't see this wording in the current standard, which, at least to
me, is confusing.

On a related issue, the current standard seems to support symmetry
between functions *scanf's and strotol, at least in regards to the
format of the expected subject sequence, yet doesn't show any support
for the symmetry in the resulting value. Most of the implementions that
I use, sscanf and strtoul will yield different results for the string
sequence "-2".

--
Al Bowers
Tampa, Fl USA
mailto: xa******@myrapidsys.com (remove the x to send email)
http://www.geocities.com/abowers822/

Nov 14 '05 #13


Al Bowers wrote:


On a related issue, the current standard seems to support symmetry
between functions *scanf's and strotol, at least in regards to the
format of the expected subject sequence, yet doesn't show any support
for the symmetry in the resulting value. Most of the implementions that
I use, sscanf and strtoul will yield different results for the string
sequence "-2".


Oops, I meant "will yield a different result for the string
"4294967297", where the results are 4294967295 (ULONG_MAX) with
strtoul and 1 with sscanf.

--
Al Bowers
Tampa, Fl USA
mailto: xa******@myrapidsys.com (remove the x to send email)
http://www.geocities.com/abowers822/

Nov 14 '05 #14
Dan Pop wrote:
.... snip ...
Unless I'm missing something, -2 is outside the range of values
that can be represented by unsigned long. And any argument about
"folding" it into range would equally apply to values above
ULONG_MAX, so this function should NEVER set errno to ERANGE if
that argument applied.


We had this argument right here and on comp.std.c a month or so
ago, and I took your side. I started it when I found that the
DJGPP implementation of strtoul did that quiet conversion. At any
rate, I got outshouted, and beaten into agreement that the action
was required by the standard.

I still think it is wrong, wrong, wrong, in fact wrong.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #15
[fu-t set]

in comp.lang.c i read:
In <m1*************@usa.net> those who know me have no need of my name
<no****************@usa.net> writes:
in comp.lang.c i read:
It still seems rather strange to me that strtoul() accepts "-2" and
returns 4294967294 without complaint, but according to the man page it
is supposed to do so.


for the same reason that assigning -2 to an unsigned long results in that
value.


If it's the same reason, then when should strtoul() set errno to ERANGE
and why?


given how negative values are handled with unsigned types and the rationale
provided for strtoul's addition i would say there is no reason it should,
despite wording which allows it.

i can envision a rationale for setting ERANGE and forcing ULONG_MAX as the
result -- when the magnitude of the value is outside 0..ULONG_MAX, or when
prefixed with a minus sign / dash outside of LONG_MIN..LONG_MAX -- but
that's just me thinking of convenience and doesn't come quickly to mind
from a plain reading of the standard.

--
a signature
Nov 14 '05 #16
In <2g************@uni-berlin.de> Al Bowers <xa******@rapidsys.com> writes:


Al Bowers wrote:
On a related issue, the current standard seems to support symmetry
between functions *scanf's and strotol, at least in regards to the
format of the expected subject sequence, yet doesn't show any support
for the symmetry in the resulting value. Most of the implementions that
I use, sscanf and strtoul will yield different results for the string
sequence "-2".


Oops, I meant "will yield a different result for the string
"4294967297", where the results are 4294967295 (ULONG_MAX) with
strtoul and 1 with sscanf.


In the case of sscanf, you're invoking undefined behaviour.

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

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

Similar topics

6
by: Amadeus W.M. | last post by:
Does strtol raise any exceptions? Thanks!
13
by: Matthias Kluwe | last post by:
Hi! In C, my everyday usage of strtol looked like using char *end; strtol( text, &end, 10 ); to read an int/long and then checking end == ( text + strlen( text ) );
11
by: nrk | last post by:
Isn't: char s = "--4"; char *endptr; strtol(s, &endptr, 0); supposed to return 0 and set endptr to s? I have run into an implementation (not gcc, gcc does what I expect) that is returning...
10
by: Peter Dunker | last post by:
Hi, I will check a String which should contain a HEX value. I know that strtol is the right function for this job. But what happens when I will check a hex string with 8 bytes? That I can...
3
by: whisper | last post by:
Hello: I am trying to write code to read in a bunch of lines from stdin, each containing a (variable) number of integers and writing each integer in a separate line to stdout. I came up the...
2
by: Marlene Stebbins | last post by:
Suppose I'm using strtol() to convert a command line string to a number and I want to check that the input to strtol() is not non-numeric. strtol() returns zero if input is non-numeric, so I can...
14
by: Kristo | last post by:
Since there's no strtoi function in standard C, I've been searching the clc archives for the proper way to store the result of strtol to an int. My search has yielded conflicting results. About...
3
by: dstevel | last post by:
The signature for strtol is: strtol( const char*, char**, int) So.. if we start with a passed "const char*" (pointer to const char), then we can't create a non-const char pointer pointer to...
8
by: lovecreatesbea... | last post by:
Does this part of C code call and check strtol() correctly? port = strtol(argv, &endptr, 10); if (argv == endptr){ fprintf(stderr, "%s\n", "Invalid port number form"); return 1; } if (port ==...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.