• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Linux下c++中的atoi、atol、atoll、atof函数调用实例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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

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

 

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#执行多条SQL语句,实现数据库事务发布时间:2022-07-13
下一篇:
C++保留小数发布时间:2022-07-13
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap