The implementation of the atoi function is very simple, and it is easy to directly copy the source code in the library. This is because a friend met during the interview and was asked when a buddy met intel a few days ago. Coincidentally, I met again today when I checked the interview questions of guanglian. Three times in a week, so I have to pay attention to it. After all, I have to start my interview questions! In fact, this function is easy to implement, but it is not so easy to write well. I wrote it myself and went to the glibc library to check it, if you find that you have not considered many cases, your code will be despised. Therefore, the code used in the glibc library is pasted here. Originally intended to paste the vxwork code, but the vxwork code is long and a little obscure, the final decision is to paste the code in the glibc library, which is more concise. In fact, both atoi and atol call the strtol function, so the code of this function is actually pasted. In fact, you can simply read the glibc Library: # define long_max 2147483647l
# Define long_min (-2147483647l-1l) Long int _ strtol_internal (const char * nptr, char ** endptr, int base, int Group)
{
Unsigned long int result = 0;
Long int Sign = 1; while (* nptr = ''| * nptr = '/t ')
+ + Nptr; If (* nptr = '-')
{
Sign =-1;
++ Nptr;
}
Else if (* nptr = '+ ')
+ + Nptr; If (* nptr <'0' | * nptr> '9 ')
{
If (endptr! = NULL)
* Endptr = (char *) nptr;
Return 0l;
} Assert (base = 0 );
Base = 10;
If (* nptr = '0 ')
{
If (nptr [1] = 'X' | nptr [1] = 'X ')
{
Base = 16;
Nptr + = 2;
}
Else
Base = 8;
} While (* nptr> = '0' & * nptr <= '9 ')
{
Unsigned long int digval = * nptr-'0 ';
If (result> long_max/10
| (Sign> 0? Result = long_max/10 & digval> long_max % 10
: (Result = (unsigned long INT) long_max + 1)/10
& Digval> (unsigned long INT) long_max + 1) % 10 )))
{
Errno = erange;
Return sign> 0? Long_max: long_min;
}
Result * = base;
Result + = digval;
++ Nptr;
} Return (long INT) Result * sign;
}
The atoi function sets the second parameter to null, and the third parameter to 10. I don't know if you have noticed any space or cross-border judgment. My classmates said that the Code he wrote was finally asked to add these things, and finally he was stuck (it was said that he was not careful enough and sweated ).
The implementation of the atoi function is very simple, and it is easy to directly copy the source code in the library. This is because a friend met during the interview and was asked when a buddy met intel a few days ago. Coincidentally, I met again today when I checked the interview questions of guanglian. Three times in a week, so I have to pay attention to it. After all, I have to start my interview questions! In fact, this function is easy to implement, but it is not so easy to write well. I wrote it myself and went to the glibc library to check it, if you find that you have not considered many cases, your code will be despised. Therefore, the code used in the glibc library is pasted here. Originally intended to paste the vxwork code, but the vxwork code is long and a little obscure, the final decision is to paste the code in the glibc library, which is more concise. In fact, both atoi and atol call the strtol function, so the code of this function is actually pasted. In fact, you can simply read the glibc Library: # define long_max 2147483647l
# Define long_min (-2147483647l-1l) Long int _ strtol_internal (const char * nptr, char ** endptr, int base, int Group)
{
Unsigned long int result = 0;
Long int Sign = 1; while (* nptr = ''| * nptr = '/t ')
+ + Nptr; If (* nptr = '-')
{
Sign =-1;
++ Nptr;
}
Else if (* nptr = '+ ')
+ + Nptr; If (* nptr <'0' | * nptr> '9 ')
{
If (endptr! = NULL)
* Endptr = (char *) nptr;
Return 0l;
} Assert (base = 0 );
Base = 10;
If (* nptr = '0 ')
{
If (nptr [1] = 'X' | nptr [1] = 'X ')
{
Base = 16;
Nptr + = 2;
}
Else
Base = 8;
} While (* nptr> = '0' & * nptr <= '9 ')
{
Unsigned long int digval = * nptr-'0 ';
If (result> long_max/10
| (Sign> 0? Result = long_max/10 & digval> long_max % 10
: (Result = (unsigned long INT) long_max + 1)/10
& Digval> (unsigned long INT) long_max + 1) % 10 )))
{
Errno = erange;
Return sign> 0? Long_max: long_min;
}
Result * = base;
Result + = digval;
++ Nptr;
} Return (long INT) Result * sign;
}
The atoi function sets the second parameter to null, and the third parameter to 10. I don't know if you have noticed any space or cross-border judgment. My classmates said that the Code he wrote was finally asked to add these things, and finally he was stuck (it was said that he was not careful enough and sweated ).