所需头文件
#include <stdlib.h>
int atoi(const char *nptr);
long atol(const char *nptr);
long long atoll(const char *nptr);
long long atoq(const char *nptr);
The atoi() function converts the initial portion of the string pointed to by nptr to int. The behavior is the same as
strtol(nptr, NULL, 10);
except that atoi() does not detect errors.
The atol() and atoll() functions behave the same as atoi(), except that they convert the initial portion of the string to their return type of long or long long. atoq() is an obsolete name for atoll()
atoi函数把nptr指针所指向的初始化部分转换成int型,这个动作类似于strtol(nptr, NULL, 10),区别是atoi没有错误检查(也就是说不会有错误返回,如果传入的指针所指向的字符串是无法转换成int型的,返回值是0),atol和atoll的动作类似与atoi,区别是把nptr指针所指向的初始化部分转换成long型和long long型,atoq是atoll的旧版本
#include <stdlib.h>
int atoi(const char *nptr);
long atol(const char *nptr);
long long atoll(const char *nptr);
long long atoq(const char *nptr);
The atoi() function converts the initial portion of the string pointed to by nptr to int. The behavior is the same as
strtol(nptr, NULL, 10);
except that atoi() does not detect errors.
The atol() and atoll() functions behave the same as atoi(), except that they convert the initial portion of the string to their return type of long or long long. atoq() is an obsolete name for atoll()
atoi函数把nptr指针所指向的初始化部分转换成int型,这个动作类似于strtol(nptr, NULL, 10),区别是atoi没有错误检查(也就是说不会有错误返回,如果传入的指针所指向的字符串是无法转换成int型的,返回值是0),atol和atoll的动作类似与atoi,区别是把nptr指针所指向的初始化部分转换成long型和long long型,atoq是atoll的旧版本
返回值:
The converted value.
转换之后的结果(少数几个没有出错返回的函数)
贴一个例子:
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char **argv)
{
if (argc != 2) {
printf("usage: ./a.out <number>\n");
}
int tmp1 = atoi(argv[1]);
long tmp2 = atol(argv[1]);
long long tmp3 = atoll(argv[1]);
printf("tmp1 = %d\ntmp2 = %ld\ntmp3 = %lld\n", tmp1, tmp2, tmp3);
return 0;
}