Kodewick
Lesson 1 of 30Contents

Hello, World!

Lesson 1 of 30 · Part 1 · Getting started · about 15 minutes

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.

main.c
1#include <stdio.h>2 3int main(void) {4    printf("Hello, World!\n");5    return 0;6}
Output
Hello, World!

A first program in C takes a few more lines than in other languages. Here is what each part does:

PartWhat 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:

main.c
1#include <stdio.h>2 3int main(void) {4    printf("No semicolon\n")5    return 0;6}
Output
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.

main.c
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}
Output
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:

Guess first

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}

One printf can hold several \n:

main.c
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}
Output
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 writeYou get
\na new line
\ta tab (jumps to the next column stop)
\"a double quote
\\a backslash
main.c
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}
Output
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.

main.c
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}
Output
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.

main.c
1#include <stdio.h>2 3int main(void) {4    printf("Start\n");5    return 0;6    printf("This line never runs\n");7}
Output
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.

Exercise 1

Your first program

Make the program print Hello, C!, then a new line.

It should print
Hello, C!
exercise1.c
1#include <stdio.h>2 3int main(void) {4 5    return 0;6}
Output
Press Run or Ctrl+Enter
Exercise 2

One printf, two lines

Use only one printf to print these two lines.

It should print
Apples
Bananas
exercise2.c
1#include <stdio.h>2 3int main(void) {4    printf("Apples");5    return 0;6}
Output
Press Run or Ctrl+Enter
Exercise 3

Fix it

This program doesn't compile. Fix it so it prints She said "it compiles".

It should print
She said "it compiles"
exercise3.c
1#include <stdio.h>2 3int main(void) {4    printf("She said "it compiles"\n");5    return 0;6}
Output
Press Run or Ctrl+Enter

What you learned

  • A C program starts running at main; #include <stdio.h> brings in printf.
  • 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.
  • printf doesn'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.