0Pricing
Zig Academy · レッスン

初めてのmain関数

最小構成のZigプログラムの構造を学びます。

「初めてのmain関数」はCoddyKit上の無料Zig Academyレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはZig Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Zig Academyコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

Where Programs Begin

Every Zig executable starts at a function named main. The compiler looks for it and runs it first when your program launches. 🚀

Import the Standard Library

Most programs begin by pulling in tools. You bind the standard library to a name using @import, a built-in that loads a module.

const std = @import("std");

Built-ins Start with @

Names beginning with @, like @import, are compiler built-ins. They are part of the language itself, not normal library functions.

Declaring main

You define main with the fn keyword. The simplest version takes no arguments and returns void, meaning it hands back no value.

pub fn main() void {
    // your code runs here
}

Why pub Matters

The pub keyword makes main visible outside its file so the runtime can call it. Without pub, the program cannot find an entry point.

void Means No Return

The void return type says main produces nothing. Many real programs instead return an error union so they can report failures.

The Function Body

Code inside the curly braces is the body. Statements there run top to bottom, exactly in the order you write them.

Add a First Statement

Inside main you can call functions. Here a single print statement greets the world from the standard library.

pub fn main() void {
    std.debug.print("Hi from Zig!\n", .{});
}

Statements End with Semicolons

Each statement finishes with a semicolon. Zig is precise about this, which keeps the boundaries between statements crystal clear.

Comments for Humans

Lines starting with two slashes are comments. Zig ignores them entirely, so use them to explain intent to future readers.

// This is a comment, ignored by the compiler
const answer = 42;

The Whole First Program

Putting it together gives a complete, minimal Zig program: one import and one main function that prints a line.

const std = @import("std");

pub fn main() void {
    std.debug.print("Hello!\n", .{});
}

Quick Check

Look at a minimal Zig program. Which keyword exposes main so the program can actually start?

Recap

A Zig program starts at main: import std, declare pub fn main() void, and fill the body with statements that run in order. 🎯

よくある質問

「初めてのmain関数」レッスンは無料ですか?

はい。「初めてのmain関数」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Zig Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Zig Academyコースには全4レッスンが含まれています。

「初めてのmain関数」で何を学びますか?

最小構成のZigプログラムの構造を学びます。 ブラウザで直接実行するハンズオンコードでZig Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Zig Academyを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのZig Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/4です。

「初めてのmain関数」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このZig Academyレッスンでコードを書いて実行できますか?

はい。すべてのZig Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. あらゆるプラットフォームにZigをインストールする
  2. 初めてのmain関数
  3. std.debug.printで出力する
  4. コンパイルと実行を1ステップで行う
← Zig Academyに戻る