Your first C program
What C is, the shape every program shares, and your first run: two lines printed with printf and a value threaded into the text.
What C is for
C is a small language that sits close to the machine. Operating systems, embedded chips and many other languages are written in it, so learning it shows you the layer most software quietly stands on.
A C program is compiled: a tool called a compiler reads your text and turns it into a file the machine can run. That step is strict, and learning to read the errors it gives you is a large part of learning the language.
The shape every program shares
Every C program begins in a function called main. The name is not a choice: the runtime looks for it and starts there. int main(void) says main hands back a whole number and takes nothing in.
Before main you pull in the tools you need. The line #include <stdio.h> brings in the standard input and output library, which is where the printing function printf lives.
Braces, semicolons and the shape of a statement
C groups statements with curly braces and ends each simple statement with a semicolon. The braces after int main(void) mark the body; the semicolon after return 0 marks the end of that statement. Indentation is for you, not for the compiler: it ignores spaces and newlines, so a program written on one long line compiles just the same. The punctuation is what carries the structure.
That is why a missing semicolon is often reported on the following line. The compiler only notices that the statement never ended when it meets the next token, so the line number points at the symptom rather than the cause. When a message makes no sense, read the line above it first.
The exit status, and what the runner sees
return 0; is not decoration. It becomes the process exit status, a small number the operating system keeps after the program ends. Zero means success by convention; anything else means the program did not finish its job. Shells use that number to decide whether to run the next command in a chain, and build tools use it to mark a step failed.
main is declared int because it has to hand that number back. If you leave the return off, the standard still supplies zero when execution reaches the closing brace, but writing it makes the ending explicit and matches what the declaration promised. Inside other functions the same keyword means something different: there it hands a value to the caller, not to the system.
From text to a running file
The compiler does not read your file in one go. The preprocessor handles the lines that begin with # first, so #include <stdio.h> is replaced by the declarations of printf and its relatives. The compiler then turns the C into machine instructions, and the linker joins those instructions with the standard library to make an executable.
That is why a missing semicolon or a misspelled function name is caught before the program runs: the compiler has to understand the whole file, not just the line it is about to execute. The output you see after pressing Run is the program that survived those checks.
The format markers you will use most
%d— a whole number, such as42or-7.%s— a piece of text (a string), such as a name.%.2f— a decimal number with two digits after the point.%%— a literal percent sign, because a bare%starts a marker.
A format marker and the value after it have to agree. %d given a string, or %s given a number, is undefined behaviour: the compiler may warn, and the program may print nonsense or crash. Count the markers and the values; they should match one for one.
A whole program
#include <stdio.h>
int main(void) {
printf("Hello from C.\n");
printf("The answer is %d\n", 42);
return 0;
}What happens when it fails
The compiler separates errors from warnings. An error stops the build; a warning says the code is legal but suspicious. Read the first error first: one missing semicolon can produce a page of follow-on messages, and fixing the first often clears the rest.
When the program runs, printf writes into a buffer rather than straight to the screen. The buffer is flushed when it fills or when the program exits normally. A crash can therefore lose the last line you expected to see — that is the buffer, not proof that the line never ran.
Try these next
- Print your name, a number and a price with one
printf, using%s,%dand%.2f. - Remove a semicolon and read the compiler message. Put it back and see the message disappear.
- Print a literal
%with%%, then print a%dwith no value after it and observe what the compiler says.
Print two lines and a number
Press Run to compile and run the program. Then change the greeting and the number, and add a third printf line of your own.
Print a small receipt
Run it, then add a third item and a total line. Keep the columns lined up by using the same width in each `%s` and `%.2f` marker.
Run the example, then change the text and the number. The output pane shows exactly what the program printed, so you can predict a change and check it a second later.
Check what you learned
One question at a time. A wrong answer explains itself — take another go.
Part 1 of 5
Where does a C program start running?
