Master C

Your first C program

2 / 193
Contents

Contents

Master C

0 of 193 complete

8 min read

main, printf, and the shape of a program

The five lines every C program starts from, and what each of them is actually asking for.

A program is a list of instructions, but a list on its own is not enough. Something has to say where the list begins, and something has to say which instructions are available in the first place. C's answer to both fits on a postcard.

#include <stdio.h>

int main(void) {
    printf("Hello.\n");
    return 0;
}
hello.c, the entire file. Nothing has been left out to make it fit.

Four things are going on there and each one has a lesson's worth of detail behind it. Taken in the order they appear.

#include <stdio.h>

printf is not part of the C language. It is a function in the standard library, and a function is a named piece of code you run by writing its name and handing it some values. The standard library is the set of ready made functions that comes with every C compiler.

Before you can call a function, the compiler must have been told that it exists and what it takes. Those descriptions live in header files, and stdio.h is the header for standard input and output. #include <stdio.h> means: find that file and paste its contents here, before the compiler reads a single line of mine.

int main(void)

When a C program runs, it starts at the function called main. Not at the top of the file: at main. Every C program has exactly one, and the name is fixed, because the code that starts your program was written long before yours and it goes looking for that name.

The int in front says that main hands back a whole number when it is done. The (void) says main takes nothing in. The braces { and } mark where the function's body begins and ends, and the body is the list of instructions it runs.

printf("Hello.\n");

printf prints. The text in quotation marks is the format string, the characters to send to the output, and the quotation marks mark where that text starts and stops. printf prints exactly those characters and then stops. It adds nothing at all.

That last sentence is where people trip. If you want the output to move to a new line at the end, you have to ask, and you ask with \n: a backslash followed by n, which C reads as one new line character rather than as two letters.

Try CodeC

Add \n to the end of the first text and run it again. Then change the second text to \ttwo and see what a tab does to it.

A backslash inside a text is always the start of an instruction to the compiler rather than a character in its own right. The pair is called an escape sequence. There are four you will use constantly.

WrittenMeans
\nmove to a new line
\ta tab, used to line columns up
\"a quotation mark that does not end the text
\\one backslash
Escape sequences, as you write them inside quotation marks.
Try CodeC

Delete the backslash in front of one of the quotation marks on the second line and press play. The error you get is worth meeting once on purpose.

return 0;

return 0; ends main and hands 0 back to whatever started the program. By convention zero means it worked and any other number means it did not. This is not decoration: a shell script, a build system or another program will read that number and decide what to do next, and a program that reports success after failing will be believed.

Every instruction line above ends in a semicolon. In C a statement ends at its semicolon, not at the end of a line, so one statement can run over several lines and two can share a line. The compiler cares about the semicolons and not at all about where you pressed return.

Why must the function be called main, spelled exactly that way?

Because the code that starts your program goes looking for that name. Before any of your instructions run, a small piece of setup code that came with the compiler gets the program ready and then calls main. It was written and built long before yours was, so the name it calls is not open to negotiation. Call your function start instead and the build fails at the last step, complaining that main was never found.

All lessons in Master C