atoi 和 atof (把數字字符串轉化爲數字儲存)

int atoi(char *s)
如果字符串內容是整數就返回該整數,否則返回0
double atof(char *s)
同上,不過返回浮點型

#include<iostream>
#include<cstdlib>
using namespace std;
int main(){
    char ch1[10] = "123456";
    int a = atoi(ch1);
    cout<<a<<endl;
    char ch2[10] = "123.456";
    a = atoi(ch2);
    double b = atof(ch2); 
    cout<<a<<endl<<b<<endl;
    char ch3[10] = "abc12";
    a = atoi(ch3);
    cout<<a;
    return 0;
} 

輸出結果:

123456
123
123.456
0

發佈了50 篇原創文章 · 獲贊 9 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章