当前位置:网站首页>atof()、atoi()、atol()函数【详解】
atof()、atoi()、atol()函数【详解】
2022-07-19 17:55:00 【圣喵】
本期介绍
主要介绍:atof()、atoi()、atol()函数是如何使用的,分别有那些功能,以及在使用时一些值得注意的点。
一、前言
C语言提供了一些可以将数字字符串转换成数字的一些库函数,例如atof()、atoi()、atol()函数。下面分别介绍这些函数的功能:
- atof()函数:将字符串转换成double型数据。
- atoi()函数:将字符串转换成int型数据。
- atol()函数:将字符串装换成long int型数据。
注意:如果输入无法转换成该类型的数据,则返回值为0。
二、atof()函数
double atof( const char *string );
在VS编译器中atof()库函数在调用前需要先引用两个头文件<stdlib.h>和<math.h>
。atof()函数需要传一个字符串作为参数,且该字符串需要具有一定的格式:
1.科学计数法形式:
[空格][+/-][数字][.数字][ {d |D |e |E }[+/-]数字]
2.小数形式:[空格][+/-][数字][.数字]
atof()会扫描参数字符串,跳过前面的空格字符,直到遇上数字或正负符号才开始做转换,而后严格按照格式要求来判断,直到格式违规或遇到字符串结束标志‘\0’才会结束转换,返回一个double类型的数据。举几个例子:
三、atoi()、atol()函数
int atoi( const char *string );
long atol( const char *string );
在VS编译器中atoi()和atol()函数需要引用一个头文件<stdlib.h>
。这两个函数在传参部分没什么差别,只是在函数返回值部分有所差别:atoi()返回的是一个int型数据,atol()返回的是一个long int型数据。
传参的字符串也有一定的格式:[空格][+/-][数字]
。和atoi()、atol()函数会先扫描参数字符串,跳过前面的空格字符,直到遇上数字或正负符号才开始做转换,而后直到遇见非数字字符或字符串结束标志‘\0’才会结束转换,返回一个数值。举几个例子:
注意:如果atof()函数、atio()函数、atol()函数的返回值大于其返回类型的最大值就会出现溢出现象,从而导致传回来数据的某部分丢失。
边栏推荐
- Preparation of PDA RBCs NPs polydopamine modified erythrocyte nanoparticles / hyaluronic acid coated brucine bovine serum protein nanoparticles
- [open source] magicdata Ramc: 180 hour Chinese conversational voice data set officially released
- ES6中的拓展运算符
- 2021-07-05
- The difference between delete and truncate in DML
- 使用 Abp.Zero 搭建第三方登录模块(四):微信小程序开发
- window常用API
- #define使用方法及模板
- 申万宏源证券开户安全吗,开户就可以买股票了吗
- /Bin/bash^m: bad interpreter: no file or directory
猜你喜欢
Welcome to ICASSP 2022 - Greetings from Magic Data
百度、阿里、腾讯,谁最先倒下?
Preparation of water-soluble sodium ferulate albumin nanoparticles /p-cs-np loaded teniposide multilayer coated serum protein nanoparticles
MultiHead-Attention和Masked-Attention的机制和原理
Magic data is listed in the top 100 digital economy of Internet Weekly 2022
《如何打一场数据挖掘赛事》入门版
Structure assignment in ES6
Mysql常用语句知识点
About GCC compilation common commands
Preparation of inh-rfp-bsa-nps loaded INH and RFP albumin nanoparticles / capataxel loaded albumin nanoparticles
随机推荐
Synchronization scripts between XSync servers
简述声波滤波器市场
mfc inifile unicode mode读取方法
UI automation tool of CSDN salary increase technology: uiautomator2
Postgresql学习04-pg_hint_plan安装及使用、Sql优化小知识
Is it safe for Shenwan Hongyuan securities to open an account? Can I buy stocks after opening an account
u-boot-1.1.6移植笔记(初级篇)
ICASSP 2022 Women Session Only in Shenzhen
PMP考试易混淆概念辨析技巧!看完不丢分!
【软件测试】—— 测试分类
[software testing] - test classification
MySQL基础——数据库索引与事务
ES6中的模板字符串,简化对象写法
Kibana索引模式数据搜索
【软件测试】—— 微信发红包测试用例
July training (day 19) - binary tree
Commonly used operations on tensor shape in pytorch
面试官必问的 3 道 MQ 面试题,还有谁不会??
#define使用方法及模板
Structure assignment in ES6