函式
<cstdlib>

strtoll

long long int strtoll (const char* str, char** endptr, int base);
將字串轉換為長長整數
該函式解析C風格字串 str,將其內容解釋為指定 base 的整數,並以 long long int 型別返回。如果 endptr 不是空指標,則該函式還會將 endptr 的值設定為指向數字之後的第一個字元。

該函式的操作類似於 strtol 來解析字串,但產生 long long int 型別的值(有關解析過程的詳細資訊,請參見 strtol)。

引數

str
以整數表示開頭的 C 字串。
endptr
char* 型別物件的引用,其值由函式設定為 str 中數值之後的下一個字元。
該引數也可以是一個空指標,此時它將不被使用。
base
決定有效字元及其解釋的數字基數(radix)。
如果為 0,則使用的基數由序列中的格式決定(有關詳細資訊,請參見 strtol)。

返回值

成功時,該函式將轉換後的整數值以 long long int 型別返回。
如果無法執行有效的轉換,則返回零值(0LL)。
如果讀取的值超出了 long long int 的可表示範圍,則函式返回 LLONG_MAXLLONG_MIN(在 <climits> 中定義),並且 errno 被設定為 ERANGE

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/* strtoll example */
#include <stdio.h>      /* printf, NULL */
#include <stdlib.h>     /* strtoll */

int main ()
{
  char szNumbers[] = "1856892505 17b00a12b -01100011010110000010001101100 0x6fffff";
  char* pEnd;
  long long int lli1, lli2, lli3, lli4;
  lli1 = strtoll (szNumbers, &pEnd, 10);
  lli2 = strtoll (pEnd, &pEnd, 16);
  lli3 = strtoll (pEnd, &pEnd, 2);
  lli4 = strtoll (pEnd, NULL, 0);
  printf ("The decimal equivalents are: %lld, %lld, %lld and %lld.\n", lli1, lli2, lli3, lli4);
  return 0;
}

可能的輸出

The decimal equivalents are: 1856892505, 6358606123, -208340076 and 7340031


資料競爭

str 指向的陣列被訪問,endptr 指向的指標被修改(如果非空)。

異常 (C++)

無異常保證:此函式從不丟擲異常。

如果 str 沒有指向一個有效的 C 字串,或者 endptr 沒有指向一個有效的指標物件,將導致未定義行為

另見