C 庫函數 - atol()
描述
C 庫函數 long int atol(const char *str) 把參數 str 所指向的字串轉換為一個長整數(類型為 long int 型)。
聲明
下麵是 atol() 函數的聲明。
long int atol(const char *str)
參數
- str -- 要轉換為長整數的字串。
返回值
該函數返回轉換後的長整數,如果沒有執行有效的轉換,則返回零。
實例
下麵的實例演示了 atol() 函數的用法。
#include <stdio.h> #include <stdlib.h> #include <string.h> int main() { long val; char str[20]; strcpy(str, "98993489"); val = atol(str); printf("字串值 = %s, 長整型值 = %ld\n", str, val); strcpy(str, "xuhuhu.com"); val = atol(str); printf("字串值 = %s, 長整型值 = %ld\n", str, val); return(0); }
讓我們編譯並運行上面的程式,這將產生以下結果:
字串值 = 98993489, 長整型值 = 98993489 字串值 = xuhuhu.com, 長整型值 = 0