atoi 等相关函数的用法
来源:互联网 发布:淘宝卖家套餐怎么设置 编辑:程序博客网 时间:2024/05/16 09:48
本文中调用的四个函数如下:
atoi函数:将字符串转化为int类型变量
atol函数:将字符串转化为long类型变量
atoll函数:将字符串转化为long long类型变量
atof函数:将字符串转化为double类型变量
这些函数的转化过程,都是将一个字符串的可读部分取到变量中
遇到不可读的部分,则直接终止读取。
调用示例:
#include <stdio.h>
#include <stdlib.h>
#define Seperate(); printf("\n=============\n\n");
int
main()
{
Seperate();
//atoi
printf
(
"atoi: string to integer\n"
);
const
char
* s00 =
"1234567890"
;
printf
(
"%s -> %d\n"
, s00,
atoi
(&s00[0]));
const
char
* s01 =
"123.4"
;
printf
(
"%s -> %d\n"
, s01,
atoi
(&s01[0]));
const
char
* s02 =
"xyz"
;
printf
(
"%s -> %d\n"
, s02,
atoi
(&s02[0]));
const
char
* s03 =
"1234xyz"
;
printf
(
"%s -> %d\n"
, s03,
atoi
(&s03[0]));
Seperate();
//atol
printf
(
"atol: string to long\n"
);
const
char
* s10 =
"1234567890123"
;
printf
(
"%s -> %ld\n"
, s10,
atol
(&s10[0]));
const
char
* s11 =
"123.4"
;
printf
(
"%s -> %ld\n"
, s11,
atol
(&s11[0]));
const
char
* s12 =
"xyz"
;
printf
(
"%s -> %ld\n"
, s12,
atol
(&s12[0]));
const
char
* s13 =
"1234xyz"
;
printf
(
"%s -> %ld\n"
, s13,
atol
(&s13[0]));
Seperate();
//atoll
printf
(
"atoll: string to long long\n"
);
const
char
* s20 =
"1234567890123"
;
printf
(
"%s -> %lld\n"
, s20, atoll(&s20[0]));
const
char
* s21 =
"123.4"
;
printf
(
"%s -> %lld\n"
, s21, atoll(&s21[0]));
const
char
* s22 =
"xyz"
;
printf
(
"%s -> %lld\n"
, s22, atoll(&s22[0]));
const
char
* s23 =
"1234xyz"
;
printf
(
"%s -> %lld\n"
, s23, atoll(&s23[0]));
Seperate();
//atof
printf
(
"atof: string to double\n"
);
const
char
* s30 =
"1234567890"
;
printf
(
"%s -> %lf\n"
, s30,
atof
(&s30[0]));
const
char
* s31 =
"123.4"
;
printf
(
"%s -> %lf\n"
, s31,
atof
(&s31[0]));
const
char
* s32 =
"xyz"
;
printf
(
"%s -> %lf\n"
, s32,
atof
(&s32[0]));
const
char
* s33 =
"1234xyz"
;
printf
(
"%s -> %lf\n"
, s33,
atof
(&s33[0]));
Seperate();
return
0;
}
运行效果:
END
----------------------------------------------------------------------------------------
另:
strtod:将字符串转换成浮点数
double
strtod
(
const
char
*nptr,
char
**endptr);
strtod()会扫描参数nptr字符串,跳过前面的空格字符,直到遇上数字或正负符号才开始做转换,到出现非数字或字符串结束时('\0')才结束转换,并将结果返回。若endptr不为NULL,则会将遇到不合条件而终止的nptr中的字符指针由endptr传回。参数nptr字符串可包含正负号、小数点或E(e)来表示指数部分。如123.456或123e-2。
strtol:根据参数base来转换成长整型数
long int strtol(const char *nptr,char **endptr,int base);
参数1:字符串起始地址
参数2:返回字符串有效数字的结束地址,这也是为什么要用二级指针的原因。
参数3:转换基数。当base=0,自动判断字符串的类型,并按10进制输出,例如"0xa",就会把字符串当做16进制处理,输出的为10
strtoul:将字符串转换成无符号长整型数
unsigned long strtoul(const char *nptr,char **endptr,int base);
参数1:字符串起始地址
参数2:返回字符串有效数字的结束地址,这也是为什么要用二级指针的原因。
参数3:转换基数。当base=0,自动判断字符串的类型,并按10进制输出,例如"0xa",就会把字符串当做16进制处理,输出的为10
----------------------------------------------------------------------------------------
double
strtod
(
const
char
*nptr,
char
**endptr);
- atoi 等相关函数的用法
- atoi函数的用法
- atoi,itoa,sprintf等函数的实现
- atoi() 与 itoa()函数的用法
- atoi(s)函数用法
- atof(),atoi(),itoa(),sprintf()等用法总结,强大的sprintf()
- 常用的函数:atoi,itoa,atof,_ttoi等
- 常用的函数:atoi,itoa,atof,_ttoi等
- 常用的函数:atoi,itoa,atof,_ttoi等
- itoa函数和atoi函数的实现和用法
- atoi() 与 itoa()函数用法
- atoi() 与 itoa()函数用法
- atoi() 与 itoa()函数用法
- ATOI的用法
- atoi的用法
- vc++ C函数atoi和itoa的用法总结(转载)
- C语言中itoa和atoi函数的用法
- 学习笔记——atoi函数的用法及用C语言实现atoi
- Python HTML编码解码
- Oracle备份与恢复介绍(物理备份与逻辑备份)
- PHP $_SERVER['SCRIPT_FILENAME'] 与 __FILE__ 的区别
- PAT-PAT (Advanced Level) Practise 1018. Public Bike Management (30) 【四星级】
- C语言 extern static使用比较
- atoi 等相关函数的用法
- Webpower携手梅花网,打造优雅的SaaS产品服务
- Android开发 模块化编程之引用本地的aar
- 求无序序列每个元素最接近的值
- PL/SQL教程
- tomcat利用setenv对内存的限制和docker中tomcat内存的优雅配置
- iOS开发笔记-tabbar图标设成用户头像
- JAVA方法耗时信息捕捉并日志
- Mapeduce编程八大步骤