Check if command line argument is integer c

int atoi(const char *nptr);I just wanted to examine whether the command also line discussion passed was an integer or not.Here is the C Code:

#include #include #incorporate int main(int argc, char *argv<>) if (argc == 1) rerevolve -1; printf ("Hai, you have executed the regime : %s ", argv<0>); if (isdigit(atoi(argv<1>))) printf ("%s is a number ", argv<1>); else printf ("%s is not a number ", argv<1>); return 0;But the output is not as meant, as soon as I am passing a valid number:

$ ./a.out 123Hai, you have executed the routine : ./a.out123 is not a number$ ./a.out addHai, you have actually executed the routine : ./a.outadd is not a numberI couldn"t number out the error.

You watching: Check if command line argument is integer c


c command-line
Share
Improve this question
Follow
asked Mar 25 "15 at 6:08
*

0aslam00aslam0
1,46744 gold badges2222 silver badges4040 bronze badges
1
Add a comment |

6 Answers 6


Active Oldest Votes
17
When you refer argv<1>, it refers to a character variety containing worth 123. isdigit attribute is identified for a single character input.So to take care of via this instance, it is much better to specify a function as follows:

bool isNumber(char number<>){ int i = 0; //checking for negative numbers if (number<0> == "-") i = 1; for (; number != 0; i++) { //if (number > "9" || number
Share
Improve this answer
Follow
edited Mar 25 "15 at 6:34
answered Mar 25 "15 at 6:15

*

niyascniyasc
4,22011 gold badge2121 silver badges4646 bronze badges
6
| Sexactly how 1 more comment
14
if (isdigit(atoi(argv<1>))) will certainly be:

if (isdigit(atoi("123")))which will be:

if (isdigit(123))which will certainly be:

if ( 0 )since 123 represents the ASCII character "{".

See more: 16 Adults Reveal The I Need To Pee Really Bad Right Now Stories


Share
Improve this answer
Follow
edited Mar 25 "15 at 6:29
answered Mar 25 "15 at 6:16
*

R SahuR Sahu
198k1313 gold badges137137 silver badges251251 bronze badges
3
Add a comment |
1
I believed I"d add somepoint to the answers currently here. In enhancement to checking for numbers in base 10, I thought it would certainly be useful to inspect for and allow hexadecimal numbers as well. I likewise permit negative numbers.

I also added a few things to check for bad input (e.g. null guideline, letters inside of a string representing a decimal number, or invalid letters inside a string representing a hexadecimal number).

Keep in mind that I use the to_lower(char c) attribute to ensure that letters representing hexadecimal will certainly be lower situation, just for convenience.

I rerevolve 1 (or true) if the string is a valid number, 0 if it isn"t. If it is a valid number, I keep the base inside the parameter base.

// Rerotate 1 if str is a number, 0 otherwise.// If str is a number, store the base (10 or 16) in param base.static int is_number(char *str, int *base){ // Check for null pointer. if (str == NULL) return 0; int i; int len = strlen(str); // Single character instance. if (len == 1) *base = 10; return isdigit(str<0>); // Hexadecimal? At this suggest, we recognize size is at least 2. if ((str<0> == "0") && (str<1> == "x")) { // Check that eextremely character is a digit or a,b,c,d,e, or f. for (i = 2; i = "0" && c = "a" && c I offered this feature like this: