Hello, compiler
A CPU does not understand C. It understands machine code: numbers that mean things like "add these two registers" or "jump to this address". A compiler is the program that translates your C source text into that machine code, once, before anything runs.
Here, your code is compiled by a real C compiler to WebAssembly, a machine code for a virtual CPU that your browser runs. On a laptop, gcc or clang would produce x86 or ARM machine code instead. Same idea, different target.
Every C program has the same skeleton:
#include <stdio.h>
int main(void) {
printf("Hello!\n");
return 0;
}
#include <stdio.h>pastes in the declarations for the standard input/output functions, includingprintf. Without it, the compiler does not know whatprintfis.mainis where your program starts. Not the top of the file:main.printfprints text.\nis the newline character; C never adds one for you.return 0;hands an exit code back to whoever started the program.0means "all good".- Every statement ends in
;. Forget one and the compiler will complain, loudly.
Compilers are strict and a bit pedantic. That is a feature: every error it catches now is a bug you do not ship later. Read the error messages, they usually point at the right line (or the one just after).
Try it yourself
Edit it. Break it. Run it again.#include <stdio.h>
int main(void) {
printf("Hello from C!\n");
printf("This text became machine code before it ran.\n");
return 0;
}
Your turn
Type it yourself. That is the whole trick.1Say hello
Print exactly Hello, C! followed by a newline.
#include <stdio.h>
int main(void) {
// print the message here
return 0;
}
2Three lines
Print these three lines, exactly as shown:
I typed this.I compiled this.I understand this.
#include <stdio.h>
int main(void) {
printf("I typed this.");
return 0;
}