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?
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 */
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With