Share via


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