atoll, _atoll_l, _wtoll, _wtoll_l

将字符串转换成 long long 整数。

语法

long long atoll(
   const char *str
);
long long _wtoll(
   const wchar_t *str
);
long long _atoll_l(
   const char *str,
   _locale_t locale
);
long long _wtoll_l(
   const wchar_t *str,
   _locale_t locale
);

参数

str
要转换的字符串。

locale
要使用的区域设置。

返回值

每个函数均返回 long long 值,此值是通过将输入字符解释为数字来生成的。 如果输入不能转换为该类型的值,则 atoll 的返回值为 0。

对于大量正整数值的溢出,atoll 将返回 LLONG_MAX,对于大量负整数值的溢出,则返回 LLONG_MIN

在所有超出范围的情况下,将 errno 设置为 ERANGE。 如果传入的参数为 NULL,则调用无效参数处理程序,如参数验证中所述。 如果允许执行继续,则这些功能将 errno 设置为 EINVAL,并返回 0。

注解

这些函数将字符串转换为long long整数值。

输入字符串是一系列字符,可以解释为指定类型的数值。 该函数在首个它无法识别为数字组成部分的字符处停止读取输入字符串。 此字符可能是终止字符串的 null 字符('\0' 或 L'\0')。

atollstr 参数具有以下形式:

[whitespace] [sign] [digits]

whitespace 由空格或制表符组成,可被忽略;sign是加号 (+) 或减号 (-);digits 是一个或多个数字。

_wtollatoll 相同,但前者将宽字符串用作参数。

这些带有 _l 后缀的函数的版本与不带有该后缀的函数版本相同,只不过前者使用传入的区域设置参数而不是当前区域设置。 有关详细信息,请参阅 Locale

默认情况下,此函数的全局状态范围限定为应用程序。 若要更改此行为,请参阅 CRT 中的全局状态

一般文本例程映射

Tchar.h 例程 _UNICODE_MBCS 未定义 _MBCS 已定义 _UNICODE 已定义
_tstoll atoll atoll _wtoll
_tstoll_l _atoll_l _atoll_l _wtoll_l
_ttoll _atoll _atoll _wtoll

要求

例程 必需的标头
atoll_atoll_l <stdlib.h>
_wtoll_wtoll_l <stdlib.h> 或 <wchar.h>

示例

此程序说明如何使用 atoll 函数将存储为字符串的数字转换为数值。

// crt_atoll.c
// Build with: cl /W4 /Tc crt_atoll.c
// This program shows how to use the atoll
// functions to convert numbers stored as
// strings to numeric values.
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>

int main(void)
{
    char *str = NULL;
    long long value = 0;

    // An example of the atoll function
    // with leading and trailing white spaces.
    str = "  -27182818284 ";
    value = atoll(str);
    printf("Function: atoll(\"%s\") = %lld\n", str, value);

    // Another example of the atoll function
    // with an arbitrary decimal point.
    str = "314127.64";
    value = atoll(str);
    printf("Function: atoll(\"%s\") = %lld\n", str, value);

    // Another example of the atoll function
    // with an overflow condition occurring.
    str = "3336402735171707160320";
    value = atoll(str);
    printf("Function: atoll(\"%s\") = %lld\n", str, value);
    if (errno == ERANGE)
    {
       printf("Overflow condition occurred.\n");
    }
}
Function: atoll("  -27182818284 ") = -27182818284
Function: atoll("314127.64") = 314127
Function: atoll("3336402735171707160320") = 9223372036854775807
Overflow condition occurred.

另请参阅

数据转换
数学和浮点支持
区域设置
_ecvt
_fcvt
_gcvt
setlocale_wsetlocale
_atodbl_atodbl_l_atoldbl_atoldbl_l_atoflt_atoflt_l