473,473 Members | 2,134 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

strtol() and zero

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 write something like this:

if((x = strtol(argv[1], NULL, 10))==0)
exit(EXIT_FAILURE);

That catches non-numeric input alright, but zero is a perfectly
good number that might be input to my program. How can I use
strtol() to convert zero while checking for non-numeric input?
Nov 14 '05 #1
2 2957
Marlene Stebbins wrote:
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 write something like this:

if((x = strtol(argv[1], NULL, 10))==0)
exit(EXIT_FAILURE);

That catches non-numeric input alright, but zero is a perfectly
good number that might be input to my program. How can I use
strtol() to convert zero while checking for non-numeric input?


Assume this prototype..

long strtol(const char *s, char **endp, int base);

Rather than passing NULL as the second argument, pass the address of
a local char* like this..

long x;
char *end;
x = strtol(argv[1], &end, 10);
if (argv[1] == end) exit(EXIT_FAILURE);

--
Joe Wright mailto:jo********@comcast.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 14 '05 #2
On Tue, 14 Dec 2004 21:47:31 GMT, Marlene Stebbins <ma*****@mail.com>
wrote in comp.lang.c:
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 write something like this:

if((x = strtol(argv[1], NULL, 10))==0)
exit(EXIT_FAILURE);

That catches non-numeric input alright, but zero is a perfectly
good number that might be input to my program. How can I use
strtol() to convert zero while checking for non-numeric input?


Explanation and illustration:

http://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 14 '05 #3

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

Similar topics

1
by: Pavel Krcmar | last post by:
Hi, I'am a little bit confused. I tried this snippet (below) and ( endptr != NULL ) is true everytime. I was looking to http://www.mkssoftware.com/docs/man3/strtol.3.asp and then to...
5
by: William Payne | last post by:
Hello, I am in the process of converting a C++ program to a C program. The user of the program is supposed to supply an integer on the command line and in the C++ version of the program I was using...
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...
16
by: David Scarlett | last post by:
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...
33
by: gk245 | last post by:
I mean, anything that follows a 0 is automatically turned into a octal number. I want to have a integer variable that will hold numbers as integers even if they begin with a zero. for example:...
47
by: sudharsan | last post by:
Hi could you please explain wat atoi( ) function is for and an example how to use it?
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
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
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...
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...
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
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.

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.