Compiling a C program
C source is text. A compiler turns it into a program the machine runs, and if it cannot, nothing runs at all. Learn the two kinds of error.
Source, compiler, program
The text you type is source code, saved in a file like main.c. The machine cannot run text. A compiler reads the whole file first and translates it into machine instructions: the program. Only then does anything run.
That order has a big consequence. In Python, a typo on line 20 only stops you when line 20 runs. In C, a typo anywhere means there is no program yet, so not even line 1 runs. No output, no half-finished run, just the compiler's message.
Two kinds of error
Compile errors come from the compiler, before running. The program is not valid C: a missing ;, a name it has never seen, the wrong number of arguments. The message names the line:
main.c(5): `coisn' undeclaredRuntime errors happen while the program runs. It compiled fine, then did something impossible, like dividing by zero or touching memory it does not own:
RuntimeError: divide by zeroA compile error is about what you wrote. A runtime error is about what happened with the values. Fix compile errors top to bottom, starting with the first one.
Run it: compiled, then run
Run it, then change one thing and run it again.
#include <stdio.h>
int main(void) {
int kills = 12;
int deaths = 4;
printf("K/D ratio: %d\n", kills / deaths);
// Try 1: delete the ; above. Nothing prints: a compile error.
// Try 2: set deaths to 0. It compiles, then stops while running.
printf("done\n");
return 0;
}
Nothing prints at all
This should print two lines, but it prints nothing, not even the first line. Run it, read the message, and fix it.
#include <stdio.h>
int main(void) {
printf("Loading level 1\n");
int coins = 30;
printf("Coins: %d\n", coins)
return 0;
}