Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Check whether an argument in command line is a number

Reply
Thread Tools

Check whether an argument in command line is a number

 
 
Kuhl
Guest
Posts: n/a
 
      04-08-2009
Hi, I am programming with C in Linux. I need to check whether an
argument in the command line is a number. For example, let's say check
whether argv[3] is a number or not. I can check it by checking and
analysing each character one by one. But this is not a good way. I
believe there must be a better and simple way to do this check. How to
do it? BTW, I only want to treat the regular notation as a number. If
the argument is scientific notation, then I don't want to treat it as
a number. Thanks.
 
Reply With Quote
 
 
 
 
nick_keighley_nospam@hotmail.com
Guest
Posts: n/a
 
      04-08-2009
On 8 Apr, 15:17, Kuhl <chen_zhi...@yahoo.com> wrote:
> Hi, I am programming with C in Linux. I need to check whether an
> argument in the command line is a number. For example, let's say check
> whether argv[3] is a number or not. I can check it by checking and
> analysing each character one by one. But this is not a good way. I
> believe there must be a better and simple way to do this check. How to
> do it? BTW, I only want to treat the regular notation as a number. If
> the argument is scientific notation, then I don't want to treat it as
> a number. Thanks.


use strtol() and check it consumes all the characters, it returns an
endptr
to indicate how much it has read
 
Reply With Quote
 
 
 
 
Bartc
Guest
Posts: n/a
 
      04-08-2009

"Kuhl" <> wrote in message
news:f3138c98-7b24-40f8-9997-...
> Hi, I am programming with C in Linux. I need to check whether an
> argument in the command line is a number. For example, let's say check
> whether argv[3] is a number or not. I can check it by checking and
> analysing each character one by one. But this is not a good way.


Actually it's not a bad way. If you need to convert the argument into a
numeric value for use in the program anyway, then use the other methods
outlined.

Otherwise checking each character of a string is '0' to '9' is not too
demanding, and allows integers of any length to be entered without worrying
about overflows. Naturally you'd put this into a function such as
isnumeric(s) returning 1 or 0.

--
Bartc

 
Reply With Quote
 
luserXtrog
Guest
Posts: n/a
 
      04-08-2009
On Apr 8, 10:10*am, "Bartc" <ba...@freeuk.com> wrote:
> "Kuhl" <chen_zhi...@yahoo.com> wrote in message
>
> news:f3138c98-7b24-40f8-9997-...
>
> > Hi, I am programming with C in Linux. I need to check whether an
> > argument in the command line is a number. For example, let's say check
> > whether argv[3] is a number or not. I can check it by checking and
> > analysing each character one by one. But this is not a good way.

>
> Actually it's not a bad way. If you need to convert the argument into a
> numeric value for use in the program anyway, then use the other methods
> outlined.
>
> Otherwise checking each character of a string is '0' to '9' is not too
> demanding, and allows integers of any length to be entered without worrying
> about overflows. Naturally you'd put this into a function such as
> isnumeric(s) returning 1 or 0.
>


Why not use isdigit for that?
And a little loop across the string?

#include <stdio.h>

int main(void) {
char *s="12345a";

do if (!isdigit(*s)) {
printf("%c is not a digit\n", *s);
} while(*++s);

return 0;
}

hth
--
lxt
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      04-09-2009
Kuhl wrote:
>
> Hi, I am programming with C in Linux. I need to check whether an
> argument in the command line is a number. For example, let's say
> check whether argv[3] is a number or not. I can check it by
> checking and analysing each character one by one. But this is not
> a good way. I believe there must be a better and simple way to do
> this check. How to do it? BTW, I only want to treat the regular
> notation as a number. If the argument is scientific notation,
> then I don't want to treat it as a number. Thanks.


Look at the strtoul and strtoull functions. If you want to accept
negative values leave out the 'u' from the function name. At
completion, if the whole argument has been used, endptr will be
left pointing at a string terminating nul. Note that strtoull (and
strtoll) are only present under C99. After the call you have the
value converted, and a suitable validity check.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.


 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      04-09-2009
Eric Sosman <> writes:
> luserXtrog wrote:
>>
>> Why not use isdigit for that?
>> And a little loop across the string?
>>
>> #include <stdio.h>
>>
>> int main(void) {
>> char *s="12345a";
>>
>> do if (!isdigit(*s)) {

>
> Perl? Ada? PL/X? Whatever the language is, it's one I don't
> speak fluently.


Eric, you're about to kick yourself. It's C. Try compiling it.

I hate the code layout, though. Rather than this:

do if (condition1) {
/* ... */
} while (condition2);

I'd write:

do {
if (condition1) {
/* ... */
}
} while (condition2);

I rarely use or even see do-while loops; combining one with an if
statement that way is understandably confusing.

> If this were C, though, you'd want `isdigit( (unsigned char)*s )'
> for reasons that have been explained many times before.
>
>> printf("%c is not a digit\n", *s);
>> } while(*++s);
>>
>> return 0;
>> }


--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      04-09-2009
Eric Sosman <> writes:
> Keith Thompson wrote:
>> Eric Sosman <> writes:
>>> luserXtrog wrote:
>>>> Why not use isdigit for that?
>>>> And a little loop across the string?
>>>>
>>>> #include <stdio.h>
>>>>
>>>> int main(void) {
>>>> char *s="12345a";
>>>>
>>>> do if (!isdigit(*s)) {
>>> Perl? Ada? PL/X? Whatever the language is, it's one I don't
>>> speak fluently.

>>
>> Eric, you're about to kick yourself. [...]

>
> You're right, as usual. (Ouch!) I mis-counted the (ouch!)
> curly brac(ouch!)kets ...


For the record, it was a prediction, not a command.

--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
luserXtrog
Guest
Posts: n/a
 
      04-09-2009
On Apr 9, 2:49*pm, Kenneth Brody <kenbr...@spamcop.net> wrote:
> Keith Thompson wrote:
> > Eric Sosman <Eric.Sos...@sun.com> writes:
> >> Keith Thompson wrote:
> >>> Eric Sosman <Eric.Sos...@sun.com> writes:
> >>>> luserXtrog wrote:
> >>>>> Why not use isdigit for that?
> >>>>> And a little loop across the string?

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

>
> >>>>> int main(void) {
> >>>>> * * char *s="12345a";

>
> >>>>> * * do if (!isdigit(*s)) {
> >>>> * * Perl? *Ada? *PL/X? *Whatever the language is, it's one I don't
> >>>> speak fluently.
> >>> Eric, you're about to kick yourself. *[...]
> >> * * You're right, as usual. *(Ouch!) *I mis-counted the (ouch!)
> >> curly brac(ouch!)kets ...

>
> > For the record, it was a prediction, not a command.

>
> And, apparently, your prediction was correct.


I'm sorry to have caused such distress. My first version had no braces
at all and was all on one line. I broke it in three to try to be nice;
reasoning that whereas do takes a single statement or block and if IS
a single statement, therefore the flow should be sufficiently clear.

Isn't it in the grammar that a block (except for a function def) is a
special case of a statement which happens not to take terminating
semicolon? I'm too lazy to look it up, but that's the shape it took in
my memory. And this meme has yet to lead me astray.

--
like strOGG
 
Reply With Quote
 
Guest
Posts: n/a
 
      04-10-2009
Kuhl <> wrote:
> Hi, I am programming with C in Linux. I need to check whether an
> argument in the command line is a number. For example, let's say check
> whether argv[3] is a number or not. I can check it by checking and
> analysing each character one by one. But this is not a good way. I
> believe there must be a better and simple way to do this check. How to
> do it? BTW, I only want to treat the regular notation as a number. If
> the argument is scientific notation, then I don't want to treat it as
> a number. Thanks.


Something self-made for this purpose is the best way, IMHO.

typedef enum { false, true } BOOL;

BOOL isnum (char *s) {
int i;
BOOL num=true;

for (; *s && num; s++) {
if ((*s < '0' || *s > '9') && *s != '.')
num=false;
}

return num;
}

--
-----BEGIN GEEK CODE BLOCK-----
GCS/CM/CC/E/IT/LS/M d-(--) C++++$ UBL++++$ P++++ L+++++$ E--- W+++ w--
PS+++ PE-- Y++ PGP+++ R++ tv-- b++>+++ D+ G>+++ e++>+++++ h* r++ z+++
------END GEEK CODE BLOCK------
 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      04-10-2009
wrote:
> Kuhl <> wrote:
>> Hi, I am programming with C in Linux. I need to check whether an
>> argument in the command line is a number. For example, let's say check
>> whether argv[3] is a number or not. I can check it by checking and
>> analysing each character one by one. But this is not a good way. I
>> believe there must be a better and simple way to do this check. How to
>> do it? BTW, I only want to treat the regular notation as a number. If
>> the argument is scientific notation, then I don't want to treat it as
>> a number. Thanks.

>
> Something self-made for this purpose is the best way, IMHO.


Only if you write it very carefully.

> typedef enum { false, true } BOOL;
>
> BOOL isnum (char *s) {
> int i;
> BOOL num=true;
>
> for (; *s && num; s++) {
> if ((*s < '0' || *s > '9') && *s != '.')
> num=false;
> }
>
> return num;
> }


Now try it with the string "..."
--
Flash Gordon
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to check whether user put a check in a toolbox CheckBox contro JB ASP .Net 1 08-26-2009 11:18 PM
How to check/validate whether the value is a whole number in C#? Siew Yee ASP .Net Web Controls 1 08-12-2005 11:02 PM
Read a file line by line with a maximum number of characters per line Hugo Java 10 10-18-2004 11:42 AM
Program cannot recognize asterisk(*) as an argument passed from the command line? Fong Java 3 05-27-2004 01:55 AM
check whether a given number is an integer Reddy ASP .Net 2 01-23-2004 03:19 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57