在C語言中,可以使用標(biāo)準(zhǔn)庫函數(shù)atoi()
來實(shí)現(xiàn)將字符串轉(zhuǎn)換為數(shù)字的功能。atoi()
函數(shù)的原型如下:
int atoi(const char *str);
該函數(shù)接受一個(gè)以null結(jié)尾的字符串作為參數(shù),然后將其轉(zhuǎn)換為對應(yīng)的整數(shù)值并返回。例如:
#include <stdio.h>
#include <stdlib.h>
int main() {
char str[] = "12345";
int num = atoi(str);
printf("The number is: %d\n", num);
return 0;
}
上述代碼將字符串"12345"轉(zhuǎn)換為整數(shù)12345并輸出。需要注意的是,如果字符串無法轉(zhuǎn)換為整數(shù),atoi()
函數(shù)將返回0。