Hi,
how will you sheck if an argument passed through the argv[] is of type integer is their a builtin function in C
Thanks
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 you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
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
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
You can adapt this:
http://faq.cprogramming.com/cgi-bin/...&id=1043284385
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
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.
My best code is written with the delete key.