atol、_atol_l、_wtol、_wtol_l

Convert a string to a long integer.

long atol(
   const char *str 
);
long _atol_l(
   const char *str,
   _locale_t locale
);
long _wtol(
   const wchar_t *str 
);
long _wtol_l(
   const wchar_t *str,
   _locale_t locale
);

参数

  • str
    要转换的字符串。

  • locale
    要使用的区域设置。

返回值

每个函数返回 long 值,此值由将输入字符作为数字解析而生成。 如果该输入无法转换为该类型的值,则 atol 的返回值为 0L。

In the case of overflow with large positive integral values, atol returns LONG_MAX; in the case of overflow with large negative integral values, LONG_MIN is returned. 在所有超出范围的情况下,errno 设置为 ERANGE。 If the parameter passed in is NULL, the invalid parameter handler is invoked, as described in 参数验证. 如果允许继续执行,则这些函数将 errno 设置为 EINVAL,并返回 0。

备注

These functions convert a character string to a long integer value (atol).

输入字符串是可被解释为指定类型的数值的字符序列。 当第一个字符不能识别为数字时,函数将停止读入输入字符串。 This character may be the NULL character ('\0' or L'\0') terminating the string.

atol 的 str 参数具有以下格式:

[whitespace] [sign] [digits]]

whitespace 包含已忽略的空格或制表符;sign 是加号 (+) 或减号 (-);而 digits 是一个或多个数字。

_wtol is identical to atol except that it takes a wide character string.

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

一般文本例程映射

TCHAR.H 例程

未定义的 & _UNICODE 和 _MBCS

已定义 _MBCS

已定义 _UNICODE

_tstol

atol

atol

_wtol

_ttol

atol

atol

_wtol

要求

例程

必需的标头

atol

<stdlib.h>

_atol_l, _wtol, _wtol_l

<stdlib.h> and <wchar.h>

示例

此过程演示如何使用 atol 函数将作为字符串存储的数字转换为数值。

// crt_atol.c
// This program shows how numbers stored as
// strings can be converted to numeric values
// using the atol functions.
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>

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

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

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

    // Another example of the atol function
    // with an overflow condition occurring.
    str = "3336402735171707160320";
    value = atol( str );
    printf( "Function: atol( \"%s\" ) = %d\n", str, value );
    if (errno == ERANGE)
    {
       printf("Overflow condition occurred.\n");
    }
}
  

.NET Framework 等效项

请参见

参考

数据转换

浮点支持

区域设置

_ecvt

_fcvt

_gcvt

setlocale、_wsetlocale

_atodbl、_atodbl_l、_atoldbl、_atoldbl_l、_atoflt _atoflt_l