atoi, _atoi_l, _wtoi, _wtoi_l
将字符串转换为整数。
int atoi(
const char *str
);
int _wtoi(
const wchar_t *str
);
int _atoi_l(
const char *str,
_locale_t locale
);
int _wtoi_l(
const wchar_t *str,
_locale_t locale
);
参数
str
将转换的字符串。locale
使用的区域设置。
返回值
每个函数返回解释生成的 int 值类型字符为数字。 ,如果输入无法转换为该类型,的值返回值为 0 atoi和 _wtoi的。
对于与用负整数值的溢出, LONG_MIN 返回。 atoi 和 _wtoi 返回 INT_MAX 和 INT_MIN 在这种情况。 在所有超出范围的情况下, errno 设置为 ERANGE。 如果传递的参数是 NULL,无效参数调用处理程序,如 参数验证所述。 如果执行允许继续,对 EINVAL 的这些功能集 errno 并且返回 0。
备注
这些函数将字符串转换为整数值 (atoi 和 _wtoi)。 输入字符串是可被解释为指定类型的一个数值字符的序列。 函数停止读取输入字符串在作为数字的一部分,它无法识别的第一个字符。 此字符可以是) 终止字符串的在 null 字符 (“\0' or L” \ 0 "。
为 atoi和 _wtoi 的 str 参数具有以下形式:
[whitespace] [sign] [digits]]
whitespace 包括空格或制表符,将忽略; sign 加号 (+) 或减号 (-);并 digits 是一个或多个数字。
这些功能的版本与 _l 后缀的相同,只不过它们使用区域设置参数而不是当前区域设置。 有关更多信息,请参见 区域设置。
一般文本例程映射
TCHAR.H 实例 |
未定义的 _UNICODE _MBCS |
定义的 _MBCS |
定义的 _UNICODE |
---|---|---|---|
_tstoi |
atoi |
atoi |
_wtoi |
_ttoi |
atoi |
atoi |
_wtoi |
要求
实例 |
必需的头 |
---|---|
atoi |
stdlib.h |
_atoi_l, _wtoi, _wtoi_l |
stdlib.h 或 wchar.h |
示例
本过程演示作为字符串存储的数字中使用 atoi 功能,如何转换为数值。
// crt_atoi.c
// This program shows how numbers
// stored as strings can be converted to
// numeric values using the atoi functions.
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
int main( void )
{
char *str = NULL;
int value = 0;
// An example of the atoi function.
str = " -2309 ";
value = atoi( str );
printf( "Function: atoi( \"%s\" ) = %d\n", str, value );
// Another example of the atoi function.
str = "31412764";
value = atoi( str );
printf( "Function: atoi( \"%s\" ) = %d\n", str, value );
// Another example of the atoi function
// with an overflow condition occuring.
str = "3336402735171707160320";
value = atoi( str );
printf( "Function: atoi( \"%s\" ) = %d\n", str, value );
if (errno == ERANGE)
{
printf("Overflow condition occurred.\n");
}
}