Step 1 of 5
Your first program
A program is a list of instructions the computer follows from top to bottom. C is a compiled language: you write the instructions as text (the source code), a tool called the compiler checks them and translates them into machine code the processor can run, and then the program runs. Every time you press Check, a real C compiler does exactly that in your browser.
The shape of every C program
#include <stdio.h>
int main(void) {
printf("Hi!\n");
return 0;
}
Hi!
Line by line:
#include <stdio.h>pulls in the standard input/output library. It tells the compiler whatprintfis. Without it the compiler doesn't know the name and reports an error.int main(void)starts a function calledmain. Every C program begins running atmain, no matter how big it is.intsays it gives back a whole number when it finishes, and(void)says it takes no inputs.- The curly braces
{ }hold the function's body: the instructions that run, in order. printf("Hi!\n");calls theprintffunction to print text. The text goes inside double quotes.\nis a newline: it ends the line, like pressing Enter.return 0;endsmainand reports "success" (0) to the operating system. A non-zero number would mean something went wrong.
Common mistakes
- Spelling and capitals matter.
Printforprintare different names, and the compiler will say something likecall to undeclared function 'print'. - Forgetting
#include <stdio.h>gives a similar error aboutprintf. - Forgetting the
;at the end of a statement. You'll practice reading that error in step 5.
Your turn: fill in the blank so the program prints Hello, World!.