Your First Program
Every C program follows the same basic shape: include the headers you need, define a main function where execution starts, and return a status code when you're done. This lesson takes the classic first program apart line by line.
Hello, world
#include <stdio.h> int main(void) { printf("Hello, world!\n"); return 0; }
Hello, world!
Line by line
| Code | Meaning |
|---|---|
#include <stdio.h> | Pulls in the declarations of the standard input/output functions, including printf. Lines starting with # are handled by the preprocessor before compiling. |
int main(void) | Defines the function main. Every program has exactly one; it's where the program starts. int means it returns a whole number; void means it takes no arguments. |
{ … } | Braces enclose the function's body. |
printf("Hello, world!\n"); | Calls printf to print text. \n is a newline. Each statement ends with a semicolon. |
return 0; | Ends main and reports success to the operating system. Any non-zero value signals failure. |
Printing several lines
#include <stdio.h> int main(void) { printf("Line one\n"); printf("Line two, "); printf("still line two\n"); puts("puts adds the newline for you"); return 0; }
Line one Line two, still line two puts adds the newline for you
printf prints exactly what you give it — without \n, the next output continues on the same line. puts prints a string followed by a newline.
Escape sequences
Some characters can't be typed directly inside a string, so they're written with a backslash:
| Sequence | Meaning |
|---|---|
\n | Newline |
\t | Tab |
\" | Double quote |
\\ | Backslash |
%% | A percent sign (inside printf, where % starts a format) |
#include <stdio.h> int main(void) { printf("Name:\tAda\n"); printf("She said \"hello\".\n"); printf("Path: C:\\temp\\notes.txt\n"); printf("Progress: 100%%\n"); return 0; }
Name: Ada She said "hello". Path: C:\temp\notes.txt Progress: 100%
Printing numbers
printf fills in values using format specifiers such as %d for whole numbers. Lesson 11 covers them all.
#include <stdio.h> int main(void) { printf("%d + %d = %d\n", 19, 23, 19 + 23); return 0; }
19 + 23 = 42
The exit status
The value returned from main goes back to whoever ran the program — the terminal, a script or a build tool. In a terminal you can see it with echo $? straight after running a program. 0 means success. (If main reaches its closing brace without a return, it returns 0 automatically, but writing it out is clearer.)
A compile error
Forget a semicolon and the compiler stops, pointing at the problem:
#include <stdio.h> int main(void) { printf("Oops\n") return 0; }
main.c:4:21: error: expected ';' after expression
The message gives the file, line and column (main.c:4:21). Read errors from the top: the first one is usually the real problem, and later ones are often knock-on effects. GCC's wording differs slightly from Clang's, but it points to the same place.
Try it yourself
Print a small receipt using tabs to line up the columns.
Item Price Coffee 3 Croissant 2 Total 5