atoi()的用法

  • Post author:
  • Post category:其他


atoi()

函数名: atoi

头文件:<stdlib.>

功能:功 能:把字符串转换成整型数,atoi()会扫描参数string字符串,跳过前面的空格字符串,直到遇上数字或正负号才开始做转换,而再遇到非数字或字符串‘\0’时才结束转换,并将结果返回,返回转换后的整型数。

用法:int atoi(const char *string)

例如:

1)

    #include<stdlib.h>
    #include<stdio.h>
    int main(void)
    {
        float n;
        char const *str="12345.67";
        n=atoi(str);
        printf("string=%sfloat=%f\n",str,n);
        return 0;
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

输出结果:

string = 12345.67 float = 12345.000000

2)


    #include<stdlib.h>
    #include<stdio.h>
    int main()
    {
        char a[]="-100";
        char b[]="123";
    int c;
        c=atoi(a)+atoi(b);
        printf("c=%d\n",c);
        return0;
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

输出结果:

c = 23