Hi,
how will you sheck if an argument passed through the argv[] is of type integer is their a builtin function in C
Thanks
Printable View
Hi,
how will you sheck if an argument passed through the argv[] is of type integer is their a builtin function in C
Thanks
All arguments passed will be character strings. So even if you called the function with:
argv[0] will point to "42", not 42.Code:%foo 42
should give you an integer, or return 0 if no numbers are found.Code:#include <stdlib.h>
...
int foo = atoi(argv[0]);
> argv[0] will point to "42", not 42.
That's argv[1]
if the argument passed is "45d" atoi("45d") will change it 45. I might be able to use sscanf() to filter that out but was just wondering if their is a neater way of doing it
Use strtol() if you want maximum information back from the conversion as to whether it was successful or not
You can adapt this:
http://faq.cprogramming.com/cgi-bin/...&id=1043284385
well here is a crude way of doing it...
I used to do it before i doscovered atoi..
I take character by cahracter and subtract the ofset ASCII value (Difference between char "1" and number 1 in the ascii chart) and check if it was inside the number range...
but this method is not recommended..
>but this method is not recommended..
Nor is atoi. Use strtol instead if you want to bulletproof your validation code.