Your First Program

Welcome to the C course! C is one of the oldest programming languages that are still in wide use: it was created in the early 1970s. It's a small language, and programs written in it run very fast. That's why large parts of operating systems such as Linux and Windows are written in it, and so is the software in microcontrollers, the tiny computers inside washing machines, cars and toys. The language C++ grew out of C, and many other languages, such as Java and JavaScript, borrowed a lot of its notation, so what you learn here will help you with them too.

In this course, you'll learn to write programs that calculate, make decisions, repeat work, and work with lists of numbers and with text. You'll split programs into smaller parts called functions, see how C works with the computer's memory, and at the end, you'll write a small project of your own. But first, your very first program.

A program is a series of statements: commands that the computer carries out one after another, from top to bottom. A computer doesn't understand C directly, though. First, a compiler has to process the program: it checks that the program is written correctly and translates it into instructions that the computer understands. On this site, the compiler is called Clang. When you click Run, the site first compiles (translates) your program, and then it runs it.

This is a whole program that prints a greeting:

#include <stdio.h>

int main(void) {
    printf("Hello!\n");
    return 0;
}

This prints:

Hello!

What the parts mean:

  • #include <stdio.h> adds the header file stdio.h (standard input/output) to the program. It is part of the standard library: the set of ready-made tools that comes with every C compiler. This file brings printf, and without this line, the compiler wouldn't know it.
  • int main(void) { } is the main part of the program, the main function. This is where the program starts: it carries out the statements between the curly braces { and }, from top to bottom. Every C program has exactly one main. You'll find out what int and (void) mean in the lesson on functions. For now, take it as a fixed frame that you write your code into.
  • printf("Hello!\n"); prints a text. The text goes in double quotes ", which show where it starts and where it ends. Programmers call a text in quotes a string. The \n at its end isn't printed: it ends the line. There's more about it in the next exercise.
  • return 0; ends main, and with it the whole program. The 0 tells the computer that the program ended fine, without a problem.
  • The semicolon ; ends a statement, like a full stop ends a sentence. Every statement inside main has one. The lines #include, int main(void) { and } aren't statements, so they don't.

C treats uppercase and lowercase letters as different, so Printf or PRINTF won't work. When there is a mistake like that, or a semicolon is missing, the compiler doesn't run the program and shows a red error message below the output. Don't worry: check the line, fix it and run the program again. Later in this lesson, you'll learn to read these messages.

One more thing: the programs on this site can only write to the output. They can't read what you type on the keyboard, so everything they work with is written right in the program.

Instructions

The editor contains the program from the example. Click Run below the editor and look at the output (on a phone, under Output). The first run may take a few seconds, while the compiler loads.

Then change the text in the quotes, so that the program prints the traditional first message of every programmer:

Hello, world!

Keep the \n at the end of the text.

Below the first printf, add a second one that prints your name on the next line. Write it above the line return 0;, because the program ends there, and statements below it would never run. Don't forget the quotes, the \n and the semicolon at the end.

Start programming for free

By signing up, you agree to the Terms of Service and Privacy Policy.

Or sign up with:

1/7