0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【C言語】コンソールに入力しよう

Last updated at Posted at 2025-12-12

はじめに

この記事について

「C言語の基礎を学ぼう」をテーマに、自身の知識 + α をアドベントカレンダーにまとめます。
25日間でC言語をマスターしよう - Qiita Advent Calendar 2025 - Qiita

こんな方を対象としています

  • コンピュータがプログラムをどのように動かしているか知りたい/知らない方

  • プログラミングをしてみたい方

  • C言語初心者の方

キーワード

  • 標準入力/出力/エラー出力

  • scanf

  • fgets

  • getchar

説明

標準入力/出力/エラー出力

プログラムには 標準入力標準出力標準エラー出力 が存在します。

  • 標準入力:ユーザーからの入力を受け取る場所

  • 標準出力:処理結果の表示先(printfの表示先)

  • 標準エラー出力:エラーの表示先

C言語では通常いずれも 実行したコンソール になります。

scanf

標準入力からの入力を受け取る関数に scanf があります。
指定する変換指定子は、基本的に printf と同じですが、一部異なるものもあるため注意が必要です。

scanf("変換指定子", 格納先のアドレス)

プログラム例です。
scanf のタイミングでプログラムの実行が止まるので、キーボードで入力しましょう。

#include <stdio.h>
int main(void) {
    int num;
    char *str;
    printf("数値を入力:");
    scanf("%d", &num);
    printf("文字列を入力:");
    scanf("%s", str);
    printf("入力した数値は%d、\n", num);
    printf("文字列は%sです。\n", str);
    return 0;
}
数値を入力:1
文字列を入力:test
入力した数値は1、文字列はtestです。

fgets

他にも標準入力からの入力を受け取る関数に fgets があります。
fgets は文字列を受け取ります。
入力元に標準入力を表す stdin を指定することで、標準入力から値を受け取ることができます。

fgets(格納先アドレス, 最大読み取りサイズ, 入力元);

プログラム例です。 scanf とは異なり、入力決定のために押下した Enterキーの改行まで読み込まれてしまう 点に注意が必要です。

#include <stdio.h>
int main(void) {
    char str[100];
    printf("文字列を入力:");
    fgets(str, sizeof(str), stdin);
    printf("入力した文字列は%sです。", str);
    return 0;
}
文字列を入力:test
入力した文字列はtest
です。

getchar

標準入力から1バイト受け取るときは getchar 関数を使用します。
getchar は引数は受け取らず、入力された値を戻り値として返却します。

#include <stdio.h>
int main(void) {
    char moji;
    printf("文字を入力:");
    moji = getchar();
    printf("入力した文字は%cです。", moji);
    return 0;
}
文字を入力:A
入力した文字はAです。

改行コードに注意

標準入力で入力した文字は バッファ と呼ばれるメモリに格納されます。
Enterキーで入力を確定するため、バッファには \n が格納されてしまいます。

scanf 等の関数は、そのバッファから値を読み込みます。
各関数には下記のような特徴があります。

関数 読み込み 読み込み後のバッファ
scanf \n を無視して読み込む \n が残る
fgets \n を読み込む \n が残らない
getchar \n を読み込む 2文字目以降が残る( \n も残る)

例えば、 scanf の後に fgets を使用すると、fgets はユーザーからの入力を待たずに、改行コードを読み込んで先の処理に進みます。

#include <stdio.h>
int main(void) {
    char str1[100], str2[100];
    printf("文字列1を入力:");
    scanf("%s", str1);
    printf("文字列2を入力:");
    fgets(str2, sizeof(str2), stdin); // 入力がスキップされる
    printf("文字列1は%sです。\n", str1);
    printf("文字列2は%sです。\n", str2);
    return 0;
}
文字列1を入力:test
文字列2を入力:文字列1はtestです。
文字列2は
です。

練習

1. 自動販売機を作ろう

自動販売機を作ろう。

  • 事前準備
    • 商品を3つ用意する。
    • 商品には名前、金額がある。
    • 商品の在庫数は考えないものとする。
  • 動作
    • 最初に一度だけユーザーが投入金額を入力する。
    • 以下の処理を繰り返す。
      • 商品の一覧と残金を表示する。
      • ユーザーが買う商品を入力する。
        • 0 を入力した場合、繰り返しを終了する。
    • 最後におつりを表示する。
    • ユーザーからの不正な入力は考慮しない。

ポイント

改行コードの問題に気を付けましょう。

解答例

#include <stdio.h>
int main(void) {
    // 商品
    char *item[3] = {"Apple", "Banana", "Cheese"};
    // 価格
    int price[3] = {150, 200, 300};
    // タイトル
    printf("*** 自動販売機 ***\n");
    // お金を投入
    int cash;
    printf("投入する金額:");
    scanf("%d", &cash);
    // 本処理
    while (1) {
        // 商品一覧表示
        printf("商品一覧\n");
        int i;
        for (i = 0; i < 3; i++) {
            printf("%d: %s %d円\n", i + 1, item[i], price[i]);
        }
        // 残金表示
        printf("残金:%d円\n", cash);
        // 購入商品選択
        int selected;
        printf("購入する商品:");
        scanf("%d", &selected);
        if (selected == 0) {
            printf("購入を終了します。\n");
            break;
        } else {
            printf("%sを購入しました。\n", item[selected - 1]);
            cash -= price[selected - 1];
        }
    }
    printf("おつりは%d円です。", cash);
    return 0;
}
*** 自動販売機 ***
投入する金額:500
商品一覧
1: Apple 150円
2: Banana 200円
3: Cheese 300円
残金:500円
購入する商品:3
Cheeseを購入しました。
商品一覧
1: Apple 150円
2: Banana 200円
3: Cheese 300円
残金:200円
購入する商品:1
Appleを購入しました。
商品一覧
1: Apple 150円
2: Banana 200円
3: Cheese 300円
残金:50円
購入する商品:0
購入を終了します。
おつりは50円です。

おわりに

ユーザーからの入力を受け付けることでプログラムの幅が大きく広がります。
簡単なゲームも作れそうですね。

参考文献

↓↓↓ はじめてプログラミングを学んだときに読んだ本です ↓↓↓
詳細(プログラミング入門 C言語)|プログラミング|情報|実教出版

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?