Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

check if a variable is a string or an integer in C

Tags:

c

I am writing a C program and I have to read parameters by command line. How can I check if the argument passed to my program is a string (that is to say an array of characters) or an integer? Is there any immediate call I can use in C?

like image 384
Saverio Avatar asked Jun 02 '26 17:06

Saverio


1 Answers

Parameters passed by command line are always strings, if you want to check if this string can be converted to integer you can use strtol:

char *ptr = argv[1];
long num;

num = strtol(ptr, &ptr, 10);
if (*ptr == '\0')
    /* arg is a number */
else
    /* arg is NOT a number */
like image 123
David Ranieri Avatar answered Jun 05 '26 09:06

David Ranieri



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!