how to determine a string to be a number?

L

lisa.lin

Hi,all
Is there any library function to tell a string to be a number?
I did't find it in <string.h>, does anyone kow?
Thanks in advance!



Lisa Lin
 
F

Flash Gordon

Hi,all
Is there any library function to tell a string to be a number?
I did't find it in <string.h>, does anyone kow?
Thanks in advance!

There are several depending on your exact requirements and they should
all be covered in your C text book.
atoi and similar, which are not recommended
strtol and similar, which allow you to do error checking
sscanf which is powerful but harder to use

If you don't have a text book I recommend buying K&R2 a copy of which I
keep on my desk. Also you should read the comp.lang.c FAQ which will
tell you what K&R2 is and answer lots of other questions.

Don't rely on reading headers to find out what functions are available.
 
N

Niklas Norrthon

Hi,all
Is there any library function to tell a string to be a number?
I did't find it in <string.h>, does anyone kow?
Thanks in advance!

lookup strtol in <stdlib.h>

/Niklas Norrthon
 
C

Christopher Benson-Manica

Flash Gordon said:
Don't rely on reading headers to find out what functions are available.

And even if the header can tell you what functions are available,
don't count on it to tell you how to use them.
 
C

Clifford Stern

Hi,all
Is there any library function to tell a string to be a number?
I did't find it in <string.h>, does anyone kow?
Thanks in advance!

It's the macro isdigit in the library for character types: ctype.h. For
example, if the string is contained in the array "num", these lines
could be used:

int l=strlen(num);
for(i=0; i<l; i++)
if (! isdigit(num))
{
printf("non-digit\n");
exit(1);
}

Clifford Stern
(e-mail address removed)
 
J

Jack Klein

Hi,all
Is there any library function to tell a string to be a number?
I did't find it in <string.h>, does anyone kow?
Thanks in advance!

It's the macro isdigit in the library for character types: ctype.h. For
example, if the string is contained in the array "num", these lines
could be used:

int l=strlen(num);
for(i=0; i<l; i++)
if (! isdigit(num))
{
printf("non-digit\n");
exit(1);
}


This can produce undefined behavior if "plain" char is signed on an
implementation and the character array 'num' happens to contain any
characters with negative values.

All of the is... and to... functions prototyped in <ctype.h> accept an
int argument, but are only defined for values in the range of 0 to
UCHAR_MAX and the single negative value represented by the macro EOF.

Change that to:

if (!isdigit((unsigned char)num))

....and the result will always be well defined.
 
K

Keith Thompson

Is there any library function to tell a string to be a number?
I did't find it in <string.h>, does anyone kow?

Can you clarify the question? Do you want to determine whether a
string looks like a number (if so, what kind? integer? real?), or do
you want to convert a string to a number? Can you give an example?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,170
Messages
2,570,925
Members
47,464
Latest member
Bobbylenly

Latest Threads

Top