Share via


atol、 _atol_l、 _wtol、 _wtol_l

將字串轉換為 [長整數。

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值所產生的解譯為數字的輸入的字元。 傳回值是 0 L,代表atol如果輸入無法轉換成該型別的值。

如果是以較大的正數、 整數值,溢位atol會傳回LONG_MAX。 具有較大的負整數值,溢位的情況下LONG_MIN會傳回。 在所有範圍外的情況下, errno設定為 [ ERANGE。 如果傳入的參數是NULL,不正確的參數處理常式會叫用,如所述參數驗證。 如果執行,則允許繼續執行,這些函式會設定errno到EINVAL ,並傳回 0。

備註

這些函式會將字元字串轉換為 [長整數值 (atol)。

輸入的字串是一連串的字元會解譯為指定之型別的數值。 此函式會停止讀取輸入無法辨識的數字的組件的第一個字元字串。 這個字元可能是NULL字元 ('\ 0' 或 '\ 0' L) 結束的字串。

str引數為atol都採用下列格式:

[whitespace] [sign] [digits]]

A whitespace包含空格或 tab 字元,會被忽略 ; sign可能是加號 (+) 或減號 (-) ; 與digits是一或多個位數字。

_wtol是相同的atol不同之處在於它是一個寬字元字串。

使用這些函式的版本_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> 和 <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