Skip to the workspace
Pebellslearn by building
Setting Up  ›  Lesson

Running Your First Program

Running your First Program 12 minA C source file is preprocessed, compiled, assembled and linked into an executable, which you then run; the compiler's first error message is the one to fix first.

c / Running Your First Program

Explore each scene at your own pace.

01 · The rule

The rule

Running Your First Program

A C source file is preprocessed, compiled, assembled and linked into an executable, which you then run; the compiler's first error message is the one to fix first.

Follow preprocessing, compilation and linking before running the executable.

With a compiler installed, the greenhouse logger can finally say something. Unlike Python, where you run the source file directly, C has a separate build step, and knowing what happens in it will explain half the error messages you ever see.

The rule

Edit, compile, run: C must become a program before it can do anything.

Save this as logger.c:

#include <stdio.h>

int main(void)
{
    printf("Greenhouse logger ready\n");
    return 0;
}

Line by line:

  • #include <stdio.h> pastes in the standard I/O header, which declares printf.
  • int main(void) is where every C program starts. It takes no arguments here and returns an int.
  • printf(...) writes text. \n is a newline.
  • return 0; tells the operating system the program succeeded. Any other value signals a problem.

Build it, then run it:

gcc -std=c17 -Wall -Wextra -o logger logger.c
./logger

-o logger names the output file. Without it, GCC on Linux and macOS calls it a.out. On Windows the program is logger.exe.

That single gcc command runs four stages:

  1. Preprocess — handle #include and other # lines.
  2. Compile — turn C into assembly.
  3. Assemble — turn assembly into an object file of machine code.
  4. Link — join your object file with the C library, where printf is implemented, to make the executable.
Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.