数字文字列をlong型の数値に変換するatol関数について記します。
c-top.html is not found or not readable.atolの書式等を以下に記します。
#include <stdlib.h>
long atol(const char *nptr);
以下にatolを使用したCサンプルコードを記します。
&ref(): File not found: "atol.c" at page "数値/文字列数字をlongに変換・atol"; (改行コードLF)
#include <stdio.h> #include <stdlib.h> int main(void) { long i,j,k,l; i = atol("987654321"); j = atol("abcdeghij"); k = atol("99887766abcdef"); l = atol("999.999"); printf("i = %ld\n", i); printf("j = %ld\n", j); printf("k = %ld\n", k); printf("l = %ld\n", l); return 0; }
コンパイルして実行した時の結果を以下に記します。
$ gcc atol.c -o atol $ ./atol i = 987654321 j = 0 k = 99887766 l = 999
サンプルコードでは、数値以外の文字列、後方が数字以外の文字列、小数点を含む文字列を対象にatol関数を実行しました。
各出力を確認しatolの動作を理解してください。
以上、atolのCサンプルコードでした。
c-btm.html is not found or not readable.