Hello, World!
Your first C program does one thing: it shows a line of text on the screen. Every piece of code on this page compiles and runs right in your browser: press Run, change something, and run it again.
1#include <stdio.h>2 3int main(void) {4 printf("Hello, World!\n");5 return 0;6}
Hello, World!
A first program in C takes a few more lines than in other languages. Here is what each part does:
| Part | What it is |
|---|---|
#include <stdio.h> | Brings in the standard input and output tools. printf is one of them. |
int main(void) | The program starts running at main. Every C program has one. |
{ } | The curly braces hold what main does. |
printf("..."); | Shows the text in quotes. \n is a new line. |
return 0; | Tells the system the program finished without trouble. |
What compiling is
C is a compiled language: your code first goes to a compiler (gcc, for example), which translates it into a program the computer can run directly. Only then does it run. Here, when you press Run, the page compiles it for you, with the same results as gcc.
The compiler is strict. Leave out one semicolon and it refuses to compile, and tells you what is wrong:
1#include <stdio.h>2 3int main(void) {4 printf("No semicolon\n")5 return 0;6}
main.c: In function 'main':
main.c:4:29: error: expected ';' before 'return'
4 | printf("No semicolon\n")
| ^
| ;
5 | return 0;
| ~~~~~~Read the second line: main.c:4:29 is the file name, line 4, character 29. After error: comes what is wrong. The compiler even points to where the ; belongs. Fix it and run it again.
Tip: When there are errors, read the first one first. The ones after it are often caused by it.
Line by line
The statements in main run from top to bottom, one after another, and each one ends with a semicolon. Press Step to watch it go line by line.
1#include <stdio.h>2 3int main(void) {4 printf("First line\n");5 printf("Second line\n");6 printf("Third line\n");7 return 0;8}
First line Second line Third line
New lines are up to you
printf does not start a new line by itself. Without \n, the next printf carries on along the same line:
How many lines does this print?
1#include <stdio.h>2 3int main(void) {4 printf("A");5 printf("B\n");6 printf("C\n");7 return 0;8}
printf("A") has no \n, so B comes right after A: it prints AB, then a new line. Then C gets a line of its own. Two lines in all.
One printf can hold several \n:
1#include <stdio.h>2 3int main(void) {4 printf("Good morning\nGood night\n");5 printf("\n");6 printf("There is an empty line above\n");7 return 0;8}
Good morning Good night There is an empty line above
Special characters
Inside a string, a backslash \ with the character after it stands for a character that is hard to type directly:
| You write | You get |
|---|---|
\n | a new line |
\t | a tab (jumps to the next column stop) |
\" | a double quote |
\\ | a backslash |
1#include <stdio.h>2 3int main(void) {4 printf("Name\tScore\n");5 printf("Ana\t95\n");6 printf("She said \"hi\"\n");7 printf("Path: C:\\code\n");8 return 0;9}
Name Score Ana 95 She said "hi" Path: C:\code
Comments
Comments are for people; the compiler skips them. From // to the end of the line is a comment, and so is everything between /* and */, even across several lines.
1#include <stdio.h>2 3int main(void) {4 // say hello first5 printf("Hello\n");6 /* not this one for now:7 printf("Goodbye\n");8 */9 return 0;10}
Hello
main and return 0
The program starts at the first statement in main and ends at return 0;. The 0 means "all went well". When a program runs in a terminal, other programs can see this number and tell whether it went wrong. For now, remember only this: every program is written this way, with return 0; at the end of main.
1#include <stdio.h>2 3int main(void) {4 printf("Start\n");5 return 0;6 printf("This line never runs\n");7}
Start
The program ends at return, so the last printf shows nothing.
Practice
Write the code, then press Check. A hint costs nothing; the answer is there when you need it.
Your first program
✓Make the program print Hello, C!, then a new line.
Hello, C!
1#include <stdio.h>2 3int main(void) {4 5 return 0;6}
One printf, two lines
✓Use only one printf to print these two lines.
Apples Bananas
1#include <stdio.h>2 3int main(void) {4 printf("Apples");5 return 0;6}
Fix it
✓This program doesn't compile. Fix it so it prints She said "it compiles".
She said "it compiles"
1#include <stdio.h>2 3int main(void) {4 printf("She said "it compiles"\n");5 return 0;6}
What you learned
- A C program starts running at
main;#include <stdio.h>brings inprintf. - C is compiled: first it is compiled, then it runs. A compile error gives the line and the reason; read the first one first.
- Every statement ends with a semicolon.
printfdoesn't start new lines by itself: write\n.\t,\"and\\stand for special characters.//and/* */are comments;return 0;means the program finished without trouble.