I don't understand the results of the following C code.
main()
{
char s[] = "AAA";
advanceString(s);
}
void advanceString(p[3])
{
int val = atoi(p);
printf("The atoi val is %d\n",val);
}
Here the atoi
value is shown as 0, but I could not figure out the exact reason.
As per my understanding, it should be the summation of decimal equivalent of each values in the array? Please correct me if I am wrong.
atoi()
converts a string representation of an integer into its value. It will not convert arbitrary characters into their decimal value. For instance:There's nothing in the standard C library that will convert "A" to 65 or "Z" to 90, you'd need to write that yourself, specifically for whatever charset you're expecting as input.
Now that you know what
atoi()
does, please don't use it to deal with numeric input in whatever you come up with. You really should deal with input not being what you expect. Hmm, what happens when I enter 65 instead of A? Teachers love to break things.atoi()
doesn't do any error checking whatsoever, which makes anything relying on it to convert arbitrary input fragile, at best. Instead, usestrtol()
(POSIX centric example):When run, this gives:
If you don't care about saving / examining the garbage, you can set the second argument to
NULL
. There is no need tofree(garbage)
. Also note, if you pass 0 as the third argument, it's assumed the input is the desired value of a decimal, hex or octal representation. If you need a radix of 10, use 10 - it will fail if the input is not as you expect.You'd also check the return value for the maximum and minimum value a
long int
can handle. However, if either are returned to indicate an error,errno
is set. An exercise for the reader is to change*input
from123abc
toabc123
.It's important to check the return, as your example shows what happens if you don't. AbcDeFg is not a string representation of an integer, and you need to deal with that in your function.
For your implementation, the most basic advice I can give you would be a series of switches, something like:
.. then just run that in a loop.
Or, pre-populate a dictionary that a lookup function could scope (better). You wouldn't need hashes, just a key -> value store since you know what it's going to contain in advance, where the standard ASCII characters are keys, and their corresponding identifiers are values.
atoi
expects its argument to be a string representation of a decimal (base-10) integer constant;AAA
is not a valid decimal integer constant, soatoi
returns 0 because it has no other way to indicate that the input is invalid.Note that
atoi
will convert up to the first character that isn't part of a valid integer constant; in other words, "123" and "123w" will both be converted to 123.Like everyone else is saying, don't use
atoi
; usestrtol
instead.Read
atoi()
as a to i (ASCII to integer).atoi()
converts a string representing a decimal number, to integer.It tries to convert the string into an integer. Since AAA cannot be converted into an integer the value is 0. Try giving it 42 or something.
See http://www.cplusplus.com/reference/clibrary/cstdlib/atoi/