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 declaresprintf.int main(void)is where every C program starts. It takes no arguments here and returns anint.printf(...)writes text.\nis 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:
- Preprocess — handle
#includeand other#lines. - Compile — turn C into assembly.
- Assemble — turn assembly into an object file of machine code.
- Link — join your object file with the C library, where
printfis implemented, to make the executable.