Hello, World! in C: A Short History and Your First Program

Programming ·

C is over fifty years old and still everywhere: operating systems, phones, cars, game engines, and the tools that run other programming languages are full of it. It’s also where the “Hello, World!” tradition comes from. Here’s the story, and how to write and run your first C program.

A short history of C

C was created by Dennis Ritchie at Bell Labs between 1969 and 1973, alongside the Unix operating system he was building with Ken Thompson. It grew out of an earlier language, B, written by Thompson, which was itself based on a language called BCPL. Ritchie added data types and other features, and the new language was named C, as the next letter after B.

In 1973, most of Unix was rewritten in C. That was unusual at the time: operating systems were normally written in assembly language, tied to one kind of computer. Writing Unix in C meant it could be moved to new machines far more easily, and C spread along with Unix to universities and companies around the world.

In 1978, Brian Kernighan and Dennis Ritchie published The C Programming Language, known simply as “K&R.” Its first example program printed the words “hello, world”, and almost every programming tutorial since has started the same way. (Kernighan had used the phrase earlier, in a 1972 tutorial for the B language.)

C was standardized by ANSI in 1989 and by ISO in 1990, and the standard has been updated several times since, most recently with C23. Many later languages, including C++, Java, JavaScript and C#, borrowed its curly-brace style.

The code

Save this in a file called hello.c:

#include <stdio.h>

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

What each part does:

  • #include <stdio.h>: brings in the standard input/output library, which provides printf.
  • int main(void): the main function, where every C program starts. int means it returns a whole number; void means it takes no inputs.
  • printf("Hello, World!\n");: prints the text. \n is a newline, so the terminal’s prompt starts on the next line.
  • return 0;: tells the operating system the program finished successfully.

Steps to run it

C is a compiled language: a program called a compiler turns your code into an executable file, which you then run.

1. Install a compiler

  • Mac: open Terminal and run xcode-select --install. This installs Apple’s command line tools, including the Clang compiler (which you can also call as gcc).
  • Linux: install GCC with your package manager. On Ubuntu or Debian: sudo apt install build-essential. On Fedora: sudo dnf install gcc.
  • Windows: the simplest options are MSYS2 (msys2.org), which provides GCC, or Visual Studio with the “Desktop development with C++” workload, which includes Microsoft’s C compiler. You can also use WSL to run Linux and follow the Linux steps.

Check it worked:

gcc --version

2. Compile

In a terminal, go to the folder containing hello.c and compile it:

cd path/to/your/folder
gcc hello.c -o hello

-o hello names the output file. If there are no mistakes, the compiler prints nothing and creates a file called hello (hello.exe on Windows).

3. Run

./hello

On Windows, run .\hello.exe in PowerShell, or hello in Command Prompt. The terminal prints:

Hello, World!

If you change the code, compile it again before running, or you’ll still be running the old version.

Hello, World! in other languages