Su primera función main
Anatomía de un programa Zig mínimo.
Su primera función main es una lección gratuita de Zig Academy en CoddyKit. Esta es la lección 2 de 4. Puedes leer la lección completa abajo gratuitamente — luego la practicas en el navegador con un editor de código integrado y un tutor de IA 24/7. Forma parte de la ruta de aprendizaje de Zig Academy, y tu progreso se sincroniza en la web y la app de CoddyKit. El curso de Zig Academy incluye 4 lecciones en total.
Partes de esta lección aún no han sido traducidas y se muestran en inglés.
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. 🎯
Preguntas frecuentes
¿La lección «Su primera función main» es gratis?
Sí — el texto completo de «Su primera función main» es gratis para leer aquí en la web. Para practicarla de forma interactiva (editor de código integrado y tutor de IA 24/7) y desbloquear el resto del curso de Zig Academy, actualiza a CoddyKit PRO. El curso de Zig Academy incluye 4 lecciones en total.
¿Qué aprenderé en «Su primera función main»?
Anatomía de un programa Zig mínimo. Practicas Zig Academy con código real que ejecutas directamente en el navegador, y un tutor de IA 24/7 responde tus preguntas mientras trabajas en la lección.
¿Necesito experiencia previa para empezar Zig Academy?
No se requiere experiencia previa. Zig Academy en CoddyKit está estructurado para principiantes hasta estudiantes avanzados, así que puedes empezar aquí o desde el inicio y avanzar a tu ritmo. Esta es la lección 2 de 4.
¿Cuánto tiempo toma la lección «Su primera función main»?
La mayoría de las lecciones de CoddyKit toman alrededor de 5–10 minutos. Cada una es compacta e interactiva, así que avanzas constantemente y retomas exactamente por donde dejaste en la web y la app.
¿Puedo escribir y ejecutar código en esta lección de Zig Academy?
Sí. Cada lección de Zig Academy incluye un editor de código integrado, así que escribes y ejecutas código real directamente en tu navegador y obtienes retroalimentación instantánea de IA — sin configuración local necesaria.
Todas las lecciones de este curso
- Instalar Zig en cualquier plataforma
- Su primera función main
- Imprimir con std.debug.print
- Compilar y ejecutar en un paso