Why use ATOI () in C to get this unexpected result?

I don’t understand the result 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 ",val);
}

The atoi value here is shown as 0, but I can’t figure it out The exact reason.
According to my understanding, it should be the sum of the decimal equivalents of each value in the array? Please correct me if I am wrong.

atoi() will The string representation of an integer is converted to its value. It does not convert any character to a decimal value. For example:

int main(void)
{
const char *string="12345";

printf("The value of %s is %d ", string, atoi(string));
< br /> return 0;
}

There is nothing in the standard C library to convert “A” to 65 or “Z” to 90, you need to write it yourself, especially for you Expect any character set as input.

Since you know what atoi() does, please don’t use it to process any numeric input you come up with. You really should process input that is not what you expect. Hmm , What happens when I enter 65 instead of A? Teachers like to break things.

atoi() does not do any error checking, which makes anything that depends on it best to convert arbitrary input fragile. Instead, use strtol() (to POSIX-centric example):

#include 
#include
#include

int main(void)
{
static const char *input ="123abc";
char *garbage = NULL;
long value = 0 ;

errno = 0;

value = strtol(input, &garbage, 0);

switch (errno) {
case ERANGE:
printf("The data could not be represented. ");
return 1;
// host-specific (GNU/Linux in my case)
case EINVAL:
printf("Unsupported base / radix. ");
return 1;
}

printf("The value is %ld, leftover garbage in the string is %s ",
// Again, host-specific, avoid trying to print NULL.
value, garbage == NULL? "N/A": garbage);

return 0;
}

At runtime, this gives:

The value is 123, leftover garbage in
the string is abc

If you don’t care about saving/checking garbage, you can set the second parameter to NULL. There is no need for free (garbage). Also note that if you pass 0 as the third parameter, the input is assumed to be the desired value represented in decimal, hexadecimal or octal. If you need a base of 10, use 10-if If the input does not meet your expectations, it will fail.

You also need to check the return value of the maximum and minimum values ​​that the long int can handle. However, if any one of them is returned indicating an error, set errno. Reader The exercise is to change the *input from 123abc to abc123.

It is very important to check the return, because your example shows what happens if you don’t do this. AbcDeFg is not a string representation of an integer, you need to in the function Deal with it.

For your implementation, the most basic advice I can give you is a series of switches, such as:

// signed, since a return value of 0 is acceptable (NULL), -1
// means failure
int ascii_to_ascii_val(const char *in)
{
switch(in) {
// 64 other cases before'A'
case'A':
return 65;
// keep going from here
default:
return -1; // failure

)

…and then run it in a loop.

Alternatively, pre-populate a dictionary whose lookup function can range (better). You don’t need a hash, only a key -> value storage, because you know what it will contain in advance, standard ASCII characters are keys, and their corresponding identifiers are values.

I don’t understand the result 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 ",val);
}

The atoi value here is shown as 0, but I can’t figure out the exact reason.
According to my understanding , Should it be the sum of the decimal equivalents of each value in the array? Please correct me if I am wrong.

atoi() converts the string representation of an integer to its value. It Will not convert any character into a decimal value. For example:

int main(void)
{
const char *string="12345 ";

printf("The value of %s is %d ", string, atoi(string));

return 0;
}< /pre>

There is nothing in the standard C library to convert "A" to 65 or "Z" to 90, you need to write it yourself, especially for any character set you expect as input.

Since you know what atoi() does, please don't use it to process any numeric input you come up with. You really should process input that is not what you expect. Well, what happens when I type 65 instead of A ? Teachers like to break things.

atoi() does not do any error checking, which makes anything that depends on it best to convert arbitrary input fragile. Instead, use strtol() (to POSIX-centric example):

#include 
#include
#include

int main(void)
{
static const char *input ="123abc";
char *garbage = NULL;
long value = 0 ;

errno = 0;

value = strtol(input, &garbage, 0);

switch (errno) {
case ERANGE:
printf("The data could not be represented. ");
return 1;
// host-specific (GNU/Linux in my case)
case EINVAL:
printf("Unsupported base / radix. ");
return 1;
}

printf("The value is %ld, leftover garbage in the string is %s ",
// Again, host-specific, avoid trying to print NULL.
value, garbage == NULL? "N/A": garbage);

return 0;
}

At runtime, this gives:

The value is 123, leftover garbage in
the string is abc

If you don’t care about saving/checking garbage, you can set the second parameter to NULL. No need Free (garbage). Also note that if you pass 0 as the third parameter, the input is assumed to be the desired value represented in decimal, hexadecimal or octal. If you need a base of 10, use 10-if the input is not If it meets your expectations, it will fail.

You also need to check the return value of the maximum and minimum that long int can handle. However, if any one of them is returned indicating an error, set errno. The reader exercise is Change the *input from 123abc to abc123.

It is important to check the return, because your example shows what happens if you don’t. AbcDeFg is not a string representation of an integer, you need to handle it in a function .

For your implementation, the most basic advice I can give you is a series of switches, such as:

// signed, since a return value of 0 is acceptable (NULL), -1
// means failure
int ascii_to_ascii_val(const char *in)
{
switch(in) {
/ / 64 other cases before'A'
case'A':
return 65;
// keep going from here
default:
return -1; // failure

}

...and then run it in a loop.

Or, pre-populate a dictionary whose lookup function can range (better). You don’t A hash is needed, only a key -> value storage, because you know what it will contain in advance, standard ASCII characters are keys, and their corresponding identifiers are values.

Leave a Comment

Your email address will not be published.