?
This document uses PHP Chinese website manual Release
在頭文件<stdlib.h>中定義 | ||
---|---|---|
int atoi(const char * str); | ||
long atol(const char * str); | ||
long long atoll(const char * str); | (自C99以來(lái)) |
解釋str指向的字節(jié)字符串中的整數(shù)值。
放棄任何空格字符,直到找到第一個(gè)非空白字符,然后采用盡可能多的字符以形成有效的整數(shù)表示并將它們轉(zhuǎn)換為整數(shù)值。有效的整數(shù)值由以下部分組成:
(可選)加號(hào)或減號(hào)
數(shù)字
str | - | 指向要解釋的以空字符結(jié)尾的字節(jié)字符串 |
---|
若成功,則返回整數(shù)值對(duì)應(yīng)str的內(nèi)容。 如果轉(zhuǎn)換后的值超出了相應(yīng)返回類型的范圍,則返回值未定義。 如果不能執(zhí)行轉(zhuǎn)換,則返回0。
#include <stdio.h>#include <stdlib.h> int main(void){ printf("%i\n", atoi(" -123junk")); printf("%i\n", atoi("0")); printf("%i\n", atoi("junk")); // no conversion can be performed printf("%i\n", atoi("2147483648")); // UB: out of range of int}
輸出:
-12300-2147483648
C11標(biāo)準(zhǔn)(ISO / IEC 9899:2011):
7.22.1.2 atoi,atol和atoll函數(shù)(p:341)
C99標(biāo)準(zhǔn)(ISO / IEC 9899:1999):
7.20.1.2 atoi,atol和atoll功能(p:307)
C89 / C90標(biāo)準(zhǔn)(ISO / IEC 9899:1990):
4.10.1.2 atoi函數(shù)
4.10.1.3 atol函數(shù)
strtolstrtoll(C99) | 將字節(jié)字符串轉(zhuǎn)換為整數(shù)值(函數(shù)) |
---|---|
strtoul strtoull(C99) | 將字節(jié)字符串轉(zhuǎn)換為無(wú)符號(hào)整數(shù)值(函數(shù)) |
| atoi,atol,atoll的C ++文檔 |