From a C file to a running program
From source file to running program
You write C instructions as text in a source file, usually with a .c ending. The file main.c is source code, not yet a runnable program. A build translates it into a form the execution environment can run.
Follow one build
- The preprocessor handles instructions such as
#include, which brings in information from a header file. - The compiler checks C syntax and translates the program. Assembly is a low-level notation for instructions; an assembler turns it into object code, instructions that still need to be connected with other program code. Tools often hide these separate steps behind one command.
- The linker connects object code with the other code it needs, including library functions. The result is an executable. Some library connections may be completed when the program loads.
- Running that executable starts the program. Its instructions can now produce output.
A library provides reusable code for programs to call. We'll use printf from the C standard library to print text. The header tells the compiler how to call it; linking connects the program to the code that does the printing.
Building is different from running
On a computer with GCC installed, this terminal command builds a file:
gcc -std=c11 -Wall -Wextra main.c -o main
gcc names the tool. -std=c11 selects the C version used in this course; -Wall -Wextra request useful warnings. main.c is the input file, and -o main names the executable. On a Unix-like system, ./main runs it. Windows uses the resulting executable's name. These local commands are optional; the lesson Run controls handle execution on the site.
If you change the source, build again. Running an old executable still runs the old instructions.
A missing semicolon can stop a build with a compiler diagnostic: a message explaining a problem in the source. A program that builds but prints the wrong donation total has a logic error. The code is valid enough to run, but it does the wrong work. Read the first diagnostic first; one mistake can cause several later messages.