(standard c libraries translation )atoi家族

所需头文件
#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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值