• atoi 和itoa用法


    1.itoa  

    在linux下没有itoa这个函数 
      
          原型:char  *itoa(int   value,char   *string,int   radix)  
               
          用法:#include   <stdlib.h>  
           
          功能:将整数value转换成字符串存入string, radix为转换时所用基数(保存到字符串中的数据的进制基数 2 8 10 16)
           
          说明:返回指向转换后的字符串的指针  
           
          举例:  

    #include<stdlib.h>    
    #include<stdio.h> 
      int main(void)    
      {    
            int  number   =   12345;    
            char string[25];     
            itoa(number, string,10);    
            printf("integer   =   %d   string   =   %s\n",   number,   string);    
            return   0;    
      }

    2.
    atoi
    字符串转换到整型数:
    int atoi(const char *nptr)
    跳过前面的空格字符,直到遇上数字或正负符号才开始做转换,而再遇到非数字或字符串结束时('\0')才结束转换,并将结果返回。

    范例
    /* 将字符串a 与字符串b转换成数字后相加*/
    #include
    mian()
    {
    char a[]=”-100”;
    char b[]=”456”;
    int c;
    c=atoi(a)+atoi(b);
    printf(c=%d\n”,c);
    }
    执行
    c=356



    在linux下没有itoa这个函数

    linux下的字符转换函数只有:

    atof 字符串转换到浮点型数

    atoi 字符串转换到整型数: int atoi(const char *nptr);跳过前面的空格字符,直到遇上数字或正负符号才开始做转换,而再遇到非数字或字符串结束时('\0')才结束转换,并将结果返回。

    atol 字符串转换到长整型数

    ecvt 浮点型数转换到字符串,取四舍五入

    fcvt 浮点型数转换到字符串,取四舍五入

    gcvt 浮点型数转换到字符串,取四舍五入

    strtod 字符串转换到浮点型数

    strtol 字符串转换到长整型数

    strtoul 字符串转换到无符号长整型数

    toascii 将整形数转换合法的ASCII字符串

  • 相关阅读:
    批量修改Linux文件夹下所有文件大小写
    如何使用malloc申请一个二位数组
    CenOS 配置C/C++语言
    C++类定义,.h文件与.cpp文件之间的关系以及条件编译
    Error while building/deploying project...When executing step "qmake"——Qt Creator快速排错
    Hello World!
    HDU1262:寻找素数对
    NOIP1998复赛:2的幂次方表示
    HDU1002 : A + B Problem II
    Open Judge2748:全排列
  • 原文地址:https://www.cnblogs.com/dabiao/p/1953858.html
Copyright © 2020-2023  润新知