【C语法学习】27 - 字符串转换为数字

1 atoi()函数

1.1 函数原型

atoi():将str指向的字符串转换为整数,函数原型如下:

int atoi(const char *str);

1.2 参数

atoi()函数只有一个参数str:

  1. 参数str是指向要转换为整数的字符串的指针,类型为char*型。

1.3 返回值

atoi()函数的返回值类型为int型。

  1. 转换成功,返回转换后的int型整数;
  2. 转换失败,返回0值。

1.4 转换机制

  1. atoi()函数将str指向的字符串转换为int型整数;
  2. atoi()函数将字符串转换为整数的过程类似于scanf()函数从stdin中读取字符赋值给变量的过程:
    (1)有效字符为±符号和数字字符0-9;
    (2)跳过所有空字符;
    (3)如果第一非空字符是无效字符,则转换失败,返回0值;
    (4)如果第一非空字符是有效字符,则转换继续,直至遇到第一个无效字符为止(无效字符包括字母、标点符号和空字符等)。

C语言标准描述如下:

1. Interprets an integer value in a byte string pointed to by str. 
2. The implied radix is always 10.
3. Discards any whitespace characters until the first non-whitespace character is found, then takes as many characters as possible to form a valid integer number representation and converts them to an integer value. 
4. The valid integer value consists of the following parts:
   --- (optional) plus or minus sign
   --- numeric digits
5. If the value of the result cannot be represented, i.e. the converted value falls out of range of the corresponding return type, the behavior is undefined.

1.5 示例

1.5.1 示例1

代码如下在这里插入代码片所示:

int main()
{

   printf("atoi(123)  = %d\n", atoi("123"));

   printf("atoi(123a) = %d\n", atoi("123a"));
   printf("atoi(a123) = %d\n", atoi("a123"));

   printf("atoi( 123) = %d\n", atoi(" 123"));
   printf("atoi(1 23) = %d\n", atoi("1 23"));

   printf("atoi(+123) = %d\n", atoi("+123"));
   printf("atoi(-123) = %d\n", atoi("-123"));

   printf("atoi(1.23) = %d\n", atoi("1.23"));

   printf("atoi(2147483648) = %d\n", atoi("2147483648"));

   return 0;
}

代码运行结果如下图所示:

在这里插入图片描述

2 atol()函数

2.1 函数原型

atol():将str指向的字符串转换为整数,函数原型如下:

long int atol(const char *str);

2.2 参数

atol()函数只有一个参数str:

  1. 参数str是指向要转换为整数的字符串的指针,类型为char*型。

2.3 返回值

atoi()函数的返回值类型为long型。

  1. 转换成功,返回转换后的long型整数;
  2. 转换失败,返回0值。

2.4 转换机制

与atoi()函数相同。

3 atoll()函数

3.1 函数原型

atoll():将str指向的字符串转换为整数,函数原型如下:

long long int atoll(const char *str);

3.2 参数

atol()函数只有一个参数str:

  1. 参数str是指向要转换为整数的字符串的指针,类型为char*型。

3.3 返回值

atoll()函数的返回值类型为long long型。

  1. 转换成功,返回转换后的long long型整数;
  2. 转换失败,返回0值。

3.4 转换机制

与atoi()函数相同。

4 atof()函数

4.1 函数原型

atof():将str指向的字符串转换为浮点数,函数原型如下:

double atof(const char *str);

4.2 参数

atof()函数只有一个参数str:

  1. 参数str是指向要转换为整数的字符串的指针,类型为char*型。

4.3 返回值

atof()函数的返回值类型为double型。

  1. 转换成功,返回转换后的double型浮点数;
  2. 转换失败,返回0值。

4.4 转换机制

转换机制与atoi()函数相同,只是浮点数和整数的有效字符不同,atof()函数能够转换的有效字符如下:

  1. ±符号;
  2. 数字0-9;
  3. 小数点;
  4. 指数指示符E或e。

4.5 示例

4.5.1 示例1

代码如下所示:

int main()
{
   printf("atof(1.23)    = %lf\n", atof("1.23"));

   printf("atof(+1.23)   = %lf\n", atof("+1.23"));
   printf("atof(-1.23)   = %lf\n", atof("-1.23"));

   printf("atof(1.23a)   = %lf\n", atof("1.23a"));
   printf("atof(a1.23)   = %lf\n", atof("a1.23"));

   printf("atof(1.23E+3) = %lf\n", atof("1.23E+3"));
   printf("atof(1.23e-3) = %lf\n", atof("1.23e-3"));

   return 0;
}

代码运行结果如下图所示:

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值