0Pricing
Zig Academy · 강의

comptime 매개변수와 값

컴파일 시간에 존재하는 값을 전달합니다.

comptime 매개변수와 값은(는) CoddyKit의 무료 Zig Academy 강의입니다. 이것은 4개 중 2번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 Zig Academy 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. Zig Academy 강의에는 총 4개의 강의가 포함되어 있습니다.

이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.

Some Arguments Must Be Known Early

Sometimes a function needs an argument the compiler can see, not just the running program. Zig lets you mark such a parameter comptime. 🧩

Marking a comptime Parameter

Write comptime before a parameter name to require that its value be known at compile-time. The caller must pass a known value.

fn repeat(comptime n: usize) void {}

The Caller Supplies a Known Value

A comptime parameter accepts only values the compiler already knows, like literals or other compile-time constants.

repeat(3);

Run-Time Values Are Rejected

Pass a value that is only known at run-time into a comptime parameter and Zig reports a clear compile error.

comptime Inside a Body

You can also force a local expression to evaluate early. A comptime block runs during compilation and produces a constant.

const table = comptime buildTable();

comptime var for Compile-Time State

A comptime var is a mutable variable that exists only during compilation. Use it to accumulate a value as compile-time code runs.

comptime var sum = 0;

Known Values Specialize Functions

Because a comptime argument is fixed, Zig can generate a tailored version of the function for that exact value.

Mix comptime and Normal Parameters

A function can take both kinds at once: a comptime parameter for the known part and ordinary parameters for run-time data.

fn scale(comptime factor: u8, x: u32) u32 {
    return x * factor;
}

Validate Arguments Early

Since the value is known, you can check it during compilation and stop the build if it is wrong, before the program ever runs. ✅

No Run-Time Cost

Work driven by a comptime parameter happens during the build. The resulting binary carries the answer with nothing left to compute.

Inside, the Parameter Is Constant

Within the function body a comptime parameter behaves like a fixed constant, so you may use it in array sizes and other compile-time spots.

fn buffer(comptime n: usize) [n]u8 {
    return undefined;
}

Quick Check

A function declares a parameter as comptime n: usize. What kind of value must the caller pass?

Recap

Mark a parameter comptime to demand a compile-time value. Zig can then validate it and specialize the function with no run-time cost. 🎯

자주 묻는 질문

“comptime 매개변수와 값” 강의는 무료인가요?

네 — “comptime 매개변수와 값” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Zig Academy 강의 전체를 잠금 해제할 수 있습니다. Zig Academy 강의에는 총 4개의 강의가 포함되어 있습니다.

“comptime 매개변수와 값”에서 뭘 배우나요?

컴파일 시간에 존재하는 값을 전달합니다. 브라우저에서 직접 실행하는 실습 코드로 Zig Academy을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.

Zig Academy을(를) 시작하는 데 경험이 필요한가요?

사전 경험은 필요하지 않습니다. CoddyKit의 Zig Academy은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 2번째 강의입니다.

“comptime 매개변수와 값” 강의는 얼마나 걸리나요?

대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.

이 Zig Academy 강의에서 코드를 작성하고 실행할 수 있나요?

네. 모든 Zig Academy 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.

이 강의의 모든 강의

  1. 컴파일 시간과 실행 시간 비교
  2. comptime 매개변수와 값
  3. 타입은 comptime 값입니다
  4. inline for와 펼친 반복문
← Zig Academy(으)로 돌아가기