atoi函数的使用(将字符串转换成整型数)

原型:

int atoi(const char *nptr);
 
头文件:#include <stdlib.h>
 

简介

atoi(ascii to integer):是把字符串转换成整型数的一个函数。atoi( ) 函数会扫描参数 nptr字符串,跳过前面的空白字符(例如空格,tab缩进等。——百度百科

栗子:

    #include<iostream>
    #include<stdio.h>
    #include<cstdlib>
    using namespace std;

    int main()
    {
        char s[101];
        int n;
        scanf("%s",s);
        n=atoi(s);
        printf("%d
",n);

        getchar();//吸收回车
        gets(s);//输入带有空格的字符串会在空格处终止
        n=atoi(s);
        printf("%d
",n);
        return 0;
    }



原文地址:https://www.cnblogs.com/LuRenJiang/p/7465685.html