本文中调用的四个函数如下:

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

 

Linux下c++中的atoi、atol、atoll、atof函数调用实例的更多相关文章

  1. dos2unix,去掉Linux下文件中的^M

    Windows系统下使用VS2010编写好的CPP文件,想放到Linux上进行编译.发现Linux上文件中的每行代码末尾都跟着^M这个符号. 为什么同一份文件在windows上和Linux上显示的不一 ...

  2. 将自己的SpringBoot应用打包发布到Linux下Docker中

    目录 将自己的SpringBoot应用打包发布到Linux下Docker中 1. 环境介绍 2. 开始前的准备 2.1 开启docker远程连接 2.2 新建SpringBoot项目 3. 开始构建我 ...

  3. 详解Linux下iptables中的DNAT与SNAT设置(转)

    详解Linux下iptables中的DNAT与SNAT设置 这篇文章主要介绍了Linux下iptables中的DNAT与SNAT设置,是Linux网络配置中的基础知识,需要的朋友可以参考下   原文连 ...

  4. Linux下Oracle中SqlPlus时上下左右键乱码问题的解决办法

    window下的sqlplus可以通过箭头键,来回看历史命令,用起来非常的方便. 但是在Linux下,会出现各种乱码,非常不方便,如下图所示,每次打错一个字符就需要重新打一遍. 解决办法:rlwrap ...

  5. [转] linux下shell中使用上下键翻出历史命名时出现^[[A^[[A^[[A^[[B^[[B的问题解决,Linux使用退格键时出现^H解决方法

    [From] https://www.zmrbk.com/post-2030.html https://blog.csdn.net/suifengshiyu/article/details/40952 ...

  6. 在Linux下使用sprintf代替atoi实现整型转化为char*

    程序中需要用到将整型转化为char*类型,然后将两个char*类型的变量拼接.将整型转化为char*自然想到了itoa函数: 头文件:#include <stdio.h> char *it ...

  7. linux下 vi中[noeol]以及出现 feff 的问题

    "uptime.py" [noeol] 69L, 2311C"system/uptime.py" 69L, 2312C 'noeol' 就是 'no end-o ...

  8. Linux 下shell中exec解析

    exec和source都属于bash内部命令(builtins commands),在bash下输入man exec或man source可以查看所有的内部命令信息.       bash shell ...

  9. Linux下多进程编程之exec函数语法及使用实例

    exec函数族 1)exec函数族说明 fork()函数用于创建一个子进程,该子进程几乎复制了父进程的全部内容,但是,这个新创建的进程如何执行呢?exec函数族就提供了一个在进程中启动另一个程序执行的 ...

随机推荐

  1. Leetcode: Guess Number Higher or Lower II

    e are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to guess ...

  2. Lintcode: Topological Sorting

    Given an directed graph, a topological order of the graph nodes is defined as follow: For each direc ...

  3. JAVA多线程与多进程

    并发与并行是两个既相似而又不相同的概念,但往往容易混为一谈,这两者究竟有什么区别呢?本文通过一个例子让你更好地理解(本文由并发编程网翻译). 现代社会是并行的:多核.网络.云计算.用户负载,并发技术对 ...

  4. 免安装版的MySQL的安装与配置

    1. 将下载的 mysql-noinstall-5.1.69-win32.zip 解压至需要安装的位置, 如: C:\Program Files; 2. 在安装文件夹下找到 my-small.ini ...

  5. CentOS 7 下引导 Windows7 启动

    Win7/CentOS7 u盘安装后会覆盖硬盘上已有系统的’引导‘ CentOS7下恢复Win7引导 编辑 # vim /etc/grub.d/40_custom 添加 menuentry 'Wind ...

  6. asp,asp.net 以表格输出excel,数据默认科学计数的解决办法

    关键字:  style="vnd.ms-excel.numberformat:@" 问题:在用table仿excel生成中经常遇到类似于身份证的长整数类型excel默认当成科学计数 ...

  7. .scss写法及如何转化为.css

    scss可视化工具:http://koala-app.com/index-zh.html scss讲解地址:http://www.cnblogs.com/adine/archive/2012/12/1 ...

  8. Debian下配置网络的方法

    1.网络配置 配置网卡修改 /etc/network/interfaces 添加如下 # #号后面是备注,不要添加哦! auto eth0 #开机自动激活 iface eth0 inte static ...

  9. umask setuid setgid

    cat /etc/bashrc if [ $UID -gt 199 ] && [ "`id -gn`" = "`id -un`" ];#用户UI ...

  10. 为什么drop table的时候要在checking permissions花很长时间?

    昨天,我drop一个表的时候在checking permissions花了20s+,这个时间花在哪里了呢?经常查找发现我的配置文件innodb_file_per_table=1的,innodb需要遍历 ...