2023年2月14日星期二

C atol() atoll() 字串轉長整數

atol() – atoll() —Convert Character String to Long or Long Long Integer

IBM說明

The atol() function converts a character string to a long value. The atoll() function converts a character string to a long long value.

The input string is a sequence of characters that can be interpreted as a numeric value of the specified return type. The function stops reading the input string at the first character that it cannot recognize as part of a number. This character can be the null character that ends the string.

我的理解:

將字串轉成長整數,分別會有 long long long 看使用在哪個平台上,需要特別注意並使用正確的變數。



(字串接受格式)

下列使用atol()為例,使用long 去接轉換後回傳值

程式碼

#include <stdlib.h>
#include <stdio.h>

int main(void)
{
    char *strnumber = " 1234567890";
    long number_out;

    number_out = atol(strnumber);
    printf("number_in:\"%s\"\t,number_out:%ld\n", strnumber, number_out);
}
執行結果


參考資料

https://www.ibm.com/docs/en/i/7.4?topic=lf-atol-atoll-convert-character-string-long-long-long-integer

沒有留言:

發佈留言

打賞按讚