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-01

はじめに

この記事について

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

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

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

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

  • C言語初心者の方

キーワード

  • printf関数

  • 変換指定子

  • エスケープ文字

説明

printf関数

printf(文字列, 1, 2, ...)

第1引数の文字列をコンソールに表示します。

第1引数の文字列内に変換指定子がある場合、そこに設定する値1、値2・・・を後続の引数に指定します。

こんな感じです。

#include <stdio.h>
int main(void) {
    printf("%sには%d人の生徒がいます。", "クラス", 35);
    return 0;
}
クラスには35人の生徒がいます。

変換指定子

上記の %s%d のことを変換指定子といいます。
変換指定子には様々なものがありますが、よく使うものをピックアップします。

変換指定子 用途
%s 文字列
%c 文字(1バイト。日本語は使用不可)
%d 整数を10進数で表示
%f 小数

こんな感じです。

#include <stdio.h>
int main(void) {
    printf("1. %s\n", "文字列"); // 文字列は""で囲む
    printf("2. %c\n", 'A'); // 文字は''で囲む
    printf("3. %d\n", 100); // 数値はそのまま
    printf("4. %f\n", 123.45);
    return 0;
}
1. 文字列
2. A
3. 100
4. 123.450000

整数、小数は桁数を指定することができます。

#include <stdio.h>
int main(void) {
    printf("1. %5d\n", 100);       // 全体5桁
    printf("2. %04d\n", 100);      // 0埋め4桁
    printf("3. %12f\n", 123.45);   // 全体12桁(小数点含む)
    printf("4. %.4f\n", 123.45);   // 小数点以下4桁
    printf("5. %12.4f\n", 123.45); // 全体12桁(小数点含む)かつ小数点以下4桁
    return 0;
}
1.   100
2. 0100
3.   123.450000
4. 123.4500
5.     123.4500

エスケープ文字

特殊な文字を表示したい場合、想定通りの動作にならないことがあります。

#include <stdio.h>
int main(void) {
    printf("% (パーセントを表示したい)");
    return 0;
}
(パーセントを表示したい)

特殊な文字を表示したい場合、エスケープ文字を使用する必要があります。

また、改行などもエスケープ文字として定義されています。

様々なものがありますが、よく使うものをピックアップします。

エスケープ文字 用途
%% % を表示
\' ' を表示
\" " を表示
\\ \ を表示
\n 改行
\t タブ文字
#include <stdio.h>
int main(void) {
    printf("文字列は\"\"で、文字は\'\'で囲みます\n");
    printf("\\nで改行します\n");
    return 0;
}
文字列は""で、文字は''で囲みます
\nで改行します

練習

1. prinfでC言語を出力しよう

実行結果が下記のようになるプログラムを作成してみよう。

#include <stdio.h>
int main(void) {
    printf("これがC言語です");
    return 0;
}

ポイント

適切な箇所に適切なエスケープ文字を使いましょう。

解答例

#include <stdio.h>
int main(void) {
    printf("#include <stdio.h>\n");
    printf("int main(void) {\n");
    printf("    printf(\"これがC言語です\");\n");
    printf("    return 0;\n");
    printf("}");
    return 0;
}
#include <stdio.h>
int main(void) {
    printf("これがC言語です");
    return 0;
}

1つのprintfでも実装可能です。

#include <stdio.h>
int main(void) {
    printf("#include <stdio.h>\nint main(void) {\n    printf(\"これがC言語です\");\n    return 0;\n}");
    return 0;
}
#include <stdio.h>
int main(void) {
    printf("これがC言語です");
    return 0;
}

(2025/12/03追記ここから)
こんな見やすい書き方もあるそうです!
コメントで教えていただきました!ありがとうございます!

#include <stdio.h>
int main(void) {
    printf(
	    "#include <stdio.h>\n"
	    "int main(void) {\n"
	    "    printf(\"これがC言語です\");\n"
	    "    return 0;\n"
	    "}"
    );
    return 0;
}
#include <stdio.h>
int main(void) {
    printf("これがC言語です");
    return 0;
}

(2025/12/03追記ここまで)

おわりに

コンソールに文字を出力するだけで多くのことに気を付ける必要があります。その分多くのことができるということですね。素晴らしい。

参考文献

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

0
0
2

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?