tags:

views:

318

answers:

3

hi i wanted to convert the argv in ansi-c into int. i am not sure of a few things...

 int main(int argc, char* argv[])

let's assume my program is run as follows ./a input1 input2.

let's assume in this case that my input1 = 12345 and input2=67890

i don't want to use atoi. i wanted to convert it all by my own. for example when i call the argv[1] is it equal to only "12345" or is it possible that it may be " 12345 "? i.e. can it have spaces from any of its sides?

the problem with atoi and sscanf is that if my input1="0" and input1="abc"

then both atoi and sscanf would return 0, if i am wrong correct me... thanks!

+1  A: 

How about scanf then?

Kimvais
`sscanf` would be better, since the OP wants to read input from `argv` (a string), but since the `*scanf` functions use `strtol` to do their conversion, and `atoi` uses `strtol` too, the OP probably doesn't want to use this. I assumed (s)he wanted to hand-roll an `atoi` (for learning, I presume).
Chris Lutz
I don't think the scanf() functions necessarily use strtol() et al - their error handling is (a lot) worse than the error handling in strtol().
Jonathan Leffler
+5  A: 

String-to-integer conversion in C standard library is performed by strtol function (or other functions from strto... group). Forget about atoi and sscanf - these functions provide no means for error detection and/or overflow protection.

Whether or not you may receive spaces around the argument depends on the system-specific conventions for passing leading and trailing spaces and, of course, on what was actually passed by the user. Normally, different OSes provide a way to pass these spaces if the user wants to do so. So it is really up to you to decide whether you consider such input correct or erroneous.

strtol will essentially allow (and ignore) leading and trailing spaces. Same is true for atoi and sscanf.

AndreyT
+3  A: 

If you really want to do this by yourself, you need to loop through the string, checking that each character is a digit. Let's assume you're using ASCII:

char *str = "624532";
int index = 0;
int value = 0;
while(str[index]) {
    if(str[index] >= 48 && str[index] < 58) {
        value = (value * 10) + (str[index] - 48);
    }
    index++;
}

Make sure you use a null-terminated string or this will run forever. It's also not going to be very helpful if your input is malformed.

David Kanarek
Let's not assume you're using ASCII, but that the C standard guarantees that '0' through '9' will be consecutive, and that using '0' instead of 48 will be both more portable and more readable. But +1 for providing code that "works with caveats" and leaving the OP to learn how the code works in order to tweak it to his/her specific needs.
Chris Lutz
Yup, that was my goal. You could also trivially adapt it to handle non consecutive digits and (less trivially) UTF8/16, but I felt that a basic outline would suffice. The lack of details leads me to suspect it is homework.
David Kanarek
The example code doesn't deal with leading or trailing spaces - which of course constitute part of the exercise.
Jonathan Leffler
Very true, I consider that part of malformed, perhaps I should have said it only works if your input is exactly a number, on the other hand coming from the command line, space is rather unusual. Thanks for pointing this out.
David Kanarek