Preprocessor, const, static, and enum
This page needs a recent browser (with SharedArrayBuffer support). Please update Chrome, Edge, Firefox or Safari to the latest version. · このページには最新のブラウザ(SharedArrayBuffer対応)が必要です。Chrome、Edge、Firefox、Safariを最新バージョンに更新してください。
English
What runs before the compiler
- Before the compiler sees your code, the preprocessor handles every line that starts with
#. - It pastes in headers, replaces macros, and removes comments — a simple text step.
- You have used it already:
#include <stdio.h>is a preprocessor command.
日本語
コンパイラ前処理
- コンパイラがコードを見る前、プレプロセッサは
#で始まる各行を処理する。 - ヘッダファイルを含め、マクロを置き換え、コメントを削除する——単純なテキスト操作である。
- すでに使用している:
#include <stdio.h>はプレプロセッサコマンドである。
English
#include and #define
#include <stdio.h>pastes the contents of a header so you can use its functions.#define PI 3.14159makesPIa macro: the preprocessor replaces everyPIwith that text.- Macros have no type and no semicolon — they are pure text replacement.
日本語
#include と #define
#include <stdio.h>はヘッダファイルの内容を含めて、その関数を使えるようにする。#define PI 3.14159はPIをマクロにする:プレプロセッサはすべてのPIをそのテキストに置き換える。- マクロには型もセミコロンもなく、純粋なテキスト置き換えである。
English
const vs #define
const double pi = 3.14159;is a real variable that cannot be changed. It has a type, so the compiler checks it.#define PI 3.14159is just text replacement, done before compiling.- Modern C usually prefers
constfor values, but#defineis still common for constants and flags.
日本語
const vs #define
const double pi = 3.14159;は真正な変数であり、変更できない。型を持つので、コンパイラがチェックする。#define PI 3.14159は単なるテキスト置き換えであり、コンパイル前に実行される。- 現代のCでは通常、値に対して
constを好むが、定数やフラグには#defineがまだ一般的である。
English
static and enum
- A
staticvariable inside a function keeps its value between calls — it is set up once, not reset every time the function runs. - An
enumgives names to a set of relatedintvalues:enum Suit { HEARTS, DIAMONDS, CLUBS, SPADES };(hereHEARTSis0). - Both make code clearer:
staticfor memory that persists,enumfor named choices.
日本語
static と enum
- 関数内の
static変数は呼び出し間で値を保持する——一度だけ初期化され、関数が実行されるたびにリセットされない。 enumは一連の関連するint値に名前をつける:enum Suit { HEARTS, DIAMONDS, CLUBS, SPADES };(ここではHEARTSは0)。- どちらもコードを明確にする:
staticは持続するメモリに、enumは名付き選択肢に使う。
#include <stdio.h>
#define GREETING "Hello"
enum Color { RED, GREEN, BLUE };
int main(void) {
printf("%s\n", GREETING); // Hello
enum Color c = GREEN;
printf("%d\n", c); // 1
return 0;
}
English
Common mistakes
#definedoes plain text replacement — wrap macro bodies in parentheses.constcannot change; astaticlocal keeps its value between calls.
日本語
よくあるミス
#defineは単純なテキスト置き換えである——マクロ本体は括弧で囲む。constは変更できない;static局所変数は呼び出し間で値を保持する。
English
Now you try
- Use the given
#defineandenumin the starters. - A
staticlocal variable remembers its value across calls. Do not write amain— the checker provides one.
日本語
あなたも試してみよう
- スタートアップファイルに与えられた
#defineとenumを使用すること。 staticローカル変数は呼び出し間でもその値を保持します。mainを書かないでください — チェッカーが用意しています。
The enum Suit { HEARTS, DIAMONDS, CLUBS, SPADES } is given. Complete const char *suit_name(enum Suit s) so it returns the name ("Hearts", "Diamonds", "Clubs", "Spades") using a switch. Do not write a main.
Click Run to see the output here. · 実行ボタンをクリックして出力を確認してください。
A macro #define PI 3.14159265358979 is given. Complete double circle_area(double r) so it returns the area of a circle, PI * r * r, using the macro. Do not write a main.
Click Run to see the output here. · 実行ボタンをクリックして出力を確認してください。
Complete int next_id(void) using a static local counter, so the first call returns 1, the next 2, then 3, and so on. Do not write a main.
Click Run to see the output here. · 実行ボタンをクリックして出力を確認してください。