博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C语言atoi()函数:将字符串转换成int(整数) 会自动把里面的非数字抛出 转换是数字的
阅读量:4167 次
发布时间:2019-05-26

本文共 737 字,大约阅读时间需要 2 分钟。

C语言atoi()函数:将字符串转换成int(整数)

头文件:#include <stdlib.h>


atoi() 函数用来将字符串转换成整数(int),其原型为:

int atoi (const char * str);


【函数说明】atoi() 函数会扫描参数 str 字符串,跳过前面的空白字符(例如空格,tab缩进等,可以通过 
 函数来检测),直到遇上数字或正负符号才开始做转换,而再遇到非数字或字符串结束时('\0')才结束转换,并将结果返回。


【返回值】返回转换后的整型数;如果 str 不能转换成 int 或者 str 为空字符串,那么将返回 0。


温馨提示:ANSI C 规范定义了 
 共6个可以将字符串转换为数字的函数,大家可以对比学习。另外在 C99 / C++11 规范中又新增了5个函数,分别是 atoll()、strtof()、strtold()、strtoll()、strtoull(),在此不做介绍,请大家自行学习。


范例:将字符串a 与字符串b 转换成数字后相加。
 
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int main ()
  4. {
  5.     int i;
  6.     char buffer[256];
  7.     printf ("Enter a number: ");
  8.     fgets (buffer, 256, stdin);
  9.     i = atoi (buffer);
  10.     printf ("The value entered is %d.", i);
  11.     system("pause");
  12.     return 0;
  13. }
执行结果:

Enter a number: 233cyuyan

The value entered is 233.

转载地址:http://gghxi.baihongyu.com/

你可能感兴趣的文章
抽象工厂+反射(一)
查看>>
12月英语--Sowing
查看>>
泛型--datatable TO List
查看>>
存储过程
查看>>
C#之导出excel
查看>>
版本控制--SVN
查看>>
泛型 VS Data Table
查看>>
CSS盒子模型
查看>>
HTML总结(一)
查看>>
3月英语--平平淡淡
查看>>
csf格式转换--逼自己一把
查看>>
ASP控件总结(一)
查看>>
Repeater&Validator控件使用
查看>>
细水翻起半点波涛--4月英语
查看>>
ASP--Active Server Pages Summary
查看>>
常见的电脑病毒
查看>>
站在巨人的肩膀上!
查看>>
2017年5月软考总结
查看>>
Node.js中运行JavaScript代码
查看>>
5月英语总结--I will do it well.
查看>>