#navi(contents-page-name): No such page: 数値

文字列数値をintに変換・atoi

数字文字列をint型の数値に変換するatoi関数について記します。

c-top.html is not found or not readable.

関連記事

atoiの書式

atoiの書式等を以下に記します。

atoiを使用したCサンプルコード

以下にatoiを使用したCサンプルコードを記します。
&ref(): File not found: "atoi.c" at page "数値/文字列数字をintに変換・atoi"; (改行コードLF)

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

int main(void)
{
    int i,j,k,l;

    i = atoi("12345");
    j = atoi("abcde");
    k = atoi("123ab");
    l = atoi("999.9");

    printf("i = %d\n", i);
    printf("j = %d\n", j);
    printf("k = %d\n", k);
    printf("l = %d\n", l);

    return 0;
}

コンパイルして実行した時の結果を以下に記します。

$ gcc atoi.c -o atoi
$ ./atoi 
i = 12345
j = 0
k = 123
l = 999

サンプルコードでは、数値以外の文字列、後方が数字以外の文字列、小数点を含む文字列を対象にatoi関数を実行しました。
各出力を確認しatoiの動作を理解してください。

以上、atoiのCサンプルコードでした。

c-btm.html is not found or not readable.

トップ   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS