zoukankan      html  css  js  c++  java
  • atoi函数和atof函数

    1.函数名:atoi
    功能:是把字符串转换成整型数的一个函数,应用在计算机程序和办公软件中
    名字来源:alphanumeric to integer
    用法:int atoi(const char *nptr);
    说明: 参数nptr字符串,如果第一个非空格字符存在,是数字或者正负号则开始做类型转换,之后检测到非数字(包括结束符 ) 字符时停止转换,返回整型数。否则,返回零。 包含在头文件stdlib.h中
    举例1:
    #include <stdlib.h>
    #include <stdio.h>
    int main(void)
    {
        int n;
        char *str = "12345.67";
        n = atoi(str);
        printf("int=%d
    ",n);
        return 0;
    }
    输出:int = 12345

    举例2:

    #include <stdlib.h>
    #include <stdio.h>
    int main()
    {
        char a[] = "-100";
        char b[] = "123";
        int c,d,e;
        c = atoi(a);
        d = atoi(b);
        e = atoi(a) + atoi(b);
        printf("c=%d d=%d e=%d ", c, d, e);
        return 0;
    }
    输出:c=-100 d=123 e=23
    2. 函数名: atof
    功 能: 把字符串转换成浮点数
    名字来源:ascii to floating point numbers 的缩写
    用 法: double atof(const char *nptr);
    举例:
    #include <stdlib.h>
    #include <stdio.h>
    int main(void)
    {
        double d;
        char *str = "12345.67";
        d = atof(str);
        printf("string=%s double=%lf
    ", str, d);
        return 0;
    }
    基本介绍 atof(将字串转换成浮点型数)
    相关函数 atoiatolstrtodstrtolstrtoul
    表头文件 #include <stdlib.h>
    定义函数 double atof(const char *nptr);
    函数说明 atof()会扫描参数nptr字符串,跳过前面的空格字符,直到遇上数字或正负符号才开始做转换,而再遇到非数字或字符串结束时('')才结束转换,并将结果返回。参数nptr字符串可包含正负号、小数点或E(e)来表示指数部分,如123.456或123e-2。
    返回值 返回转换后的浮点型数。
    附加说明 atof()与使用strtod(nptr,(char**)NULL)结果相同。
     
  • 相关阅读:
    实验 6 数组1输出最大值和它所对应的下标
    实验5第二题
    实验5第一题
    作业 3 应用分支与循环结构解决问题 判断是否闰年
    心得与体会1
    第七章
    第六章
    第五章
    第四章
    第一章
  • 原文地址:https://www.cnblogs.com/try-again/p/4998308.html
Copyright © 2011-2022 走看看