0Pricing
Zig Academy · Lektion

Ihre erste main-Funktion

Der Aufbau eines minimalen Zig-Programms.

Ihre erste main-Funktion ist eine kostenlose Zig Academy-Lektion auf CoddyKit. Dies ist Lektion 2 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Zig Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Zig Academy-Kurs umfasst insgesamt 4 Lektionen.

Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.

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. 🎯

Häufig gestellte Fragen

Ist die Lektion „Ihre erste main-Funktion“ kostenlos?

Ja — der vollständige Text von „Ihre erste main-Funktion“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Zig Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Zig Academy-Kurs umfasst insgesamt 4 Lektionen.

Was lerne ich in „Ihre erste main-Funktion“?

Der Aufbau eines minimalen Zig-Programms. Du übst Zig Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.

Brauche ich Erfahrung, um Zig Academy zu starten?

Keine Vorkenntnisse erforderlich. Zig Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 2 von 4.

Wie lange dauert die Lektion „Ihre erste main-Funktion“?

Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.

Kann ich in dieser Zig Academy-Lektion Code schreiben und ausführen?

Ja. Jede Zig Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.

Alle Lektionen in diesem Kurs

  1. Zig auf jeder Plattform installieren
  2. Ihre erste main-Funktion
  3. Mit std.debug.print ausgeben
  4. In einem Schritt kompilieren und ausführen
← Zurück zu Zig Academy