473,786 Members | 2,712 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to determine a string to be a number?

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

Nov 15 '05 #1
6 2635
li******@zoran. com wrote:
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.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Nov 15 '05 #2
Flash Gordon <sp**@flash-gordon.me.uk> wrote:
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.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cybers pace.org | don't, I need to know. Flames welcome.
Nov 15 '05 #3
li******@zoran. com writes:
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
Nov 15 '05 #4
On 19 Oct 2005 23:34:29 -0700, li******@zoran. com wrote:
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[i]))
{
printf("non-digit\n");
exit(1);
}

Clifford Stern
ax***@lafn.org
Nov 15 '05 #5
On Thu, 20 Oct 2005 18:23:06 GMT, ax***@lafn.org (Clifford Stern)
wrote in comp.lang.c:
On 19 Oct 2005 23:34:29 -0700, li******@zoran. com wrote:
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[i]))
{
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((unsi gned char)num[i]))

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

--
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.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 15 '05 #6
li******@zoran. com writes:
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?

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #7

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

Similar topics

17
6153
by: John Bentley | last post by:
John Bentley: INTRO The phrase "decimal number" within a programming context is ambiguous. It could refer to the decimal datatype or the related but separate concept of a generic decimal number. "Decimal Number" sometimes serves to distinguish Base 10 numbers, eg "15", from Base 2 numbers, Eg "1111". At other times "Decimal Number" serves to differentiate a number from an integer. For the rest of this post I shall only use either...
1
24132
by: Me | last post by:
Hi all, I have an Access application that has to import hundreds of other MDB's, some access 97 and some 2000, and merge them into several large mdb's. so far so good, but my command: Set appAccess = GetObject(TargetDBPathNAme, "Access.Application.9") sometimes fails, of course, when the MDB to be imported is Access 97
6
1782
by: SStory | last post by:
Can anyone tell me what algorithm I should use to determine how many tabs to add to a string. I have two strings. I want to add them to a listbox concatenated but with a tab between them. one tab makes them not line up as string A and B are variable length. String A is the shortest of the 2 and the Key.
16
3430
by: Jm | last post by:
Hi All Is it possible to determine who is logged onto a machine from inside a service using code in vb.net ? I have found some code that seems to work under vb6, but doesnt under .NET ? Any help is greatly appreciated Thanks
13
3660
by: coinjo | last post by:
Is there any function to determine the length of an integer string?
2
1681
by: kathy | last post by:
what is the better way to determine if a string is number or not?
25
6549
by: lovecreatesbeauty | last post by:
Hello experts, I write a function named palindrome to determine if a character string is palindromic, and test it with some example strings. Is it suitable to add it to a company/project library as a small tool function according to its quality? I will be very happy to get your suggestion from every aspect on it: interface design, C language knowledge or algorithm efficient. Sincerely,
6
14373
by: Jana | last post by:
Greetings Access Gurus! I am working on an app to send batch transactions to our bank, and the bank requires that we place an effective date on our files that is 'one business day in the future, excluding holidays and weekends.' I didn't want to build a table of holidays that would have to be continuously updated, so I searched high and low for a function that would tell me whether a given date was a holiday, to no avail. I did find an...
9
2682
by: | last post by:
I am interested in scanning web pages for content of interest, and then auto-classifying that content. I have tables of metadata that I can use for the classification, e.g. : "John P. Jones" "Jane T. Smith" "Fred Barzowsky" "Department of Oncology" "Office of Student Affairs" "Lewis Hall" etc. etc. etc. I am wondering what the efficient way to do this in code might be. The dumb and brute-force way would be to loop through the content...
0
9497
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10363
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10110
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9964
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8993
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6749
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5398
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5534
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3670
muto222
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.