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.
한국어
컴파일러 이전 단계
- 컴파일러가 소스를 보기 전에, 전처리기(preprocessor) 는
#로 시작하는 모든 줄을 처리합니다. - 헤더 파일을 붙여넣고, 매크로를 대체하며, 주석을 제거합니다 — 단순한 텍스트 단계입니다.
- 이미 사용해 보았습니다:
#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변수는 호출 간(value between calls)에 값을 유지합니다 — 한 번만 설정되며, 함수가 매번 실행될 때마다 초기화되지 않습니다. 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는 plain text 치환입니다 — 매크로 본문을 괄호로 감싸십시오.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. · 출력을 보려면 '실행'을 클릭하세요.