Ever heard of Hello World? This sample code is a programmer’s rite of passage, but what does it mean and why do we use it? Discover the program’s history and see how it reveals various language features.
What Is Hello World?
Whether you’re new to programming, or you’re learning your seventh language, a simple sample program helps. Running any program at all is a big step, so it’s wise to start with a tried-and-tested example. That example is Hello World.
Hello World is about the most straightforward piece of code you can imagine. It has a very simple list of requirements:
- Print the text “hello, world” to the screen.
And that’s it! Of course, the exact text—including its punctuation—varies and isn’t that important. “Hello, world” is just a fun, positive thing to see when you run your first program.
It’s tricky to pinpoint the exact genesis of Hello World, but the earliest concrete example we have is from the book ‘A Tutorial Introduction to the Language B’ (1972):
main( ) {
extrn a, b, c;
putchar(a); putchar(b); putchar(c); putchar('!*n');
}
a 'hell';
b 'o, w';
c 'orld';
B is an ancient language, the precursor to C. Hello World may actually date back even earlier, to 1967 and a language called BCPL, but there’s no firm evidence of this.
The concept really became well known with the publication of The C Programming Language in 1978. This book is now one of the classic computer science texts; by including a Hello World program as its first code sample, it established a tradition that persists today.
What’s So Useful About Hello World?
Hello World has two purposes. First, it acts as a useful canonical example with which to compare languages. Because the program’s purpose is so simple and well-understood, it can teach a lot about the syntax of a language. An experienced programmer can look at a Hello World program, written in a language they know nothing about, and start learning something about it.
But Hello World also has practical applications. Getting this sample program running is a big step in writing any other program in the same language. It requires an appropriate compiler or interpreter to run the program. It will confirm that the underlying operating system correctly handles its output. And it can help you configure your IDE to work with the language.
Hello World in Several Languages
You can learn a lot about a language from its implementation of Hello World. Since each of these programs should behave exactly the same, you can also use a set of hello.* source files to test your environment.
Hello World in C
As mentioned, the first reliable example of Hello World we know about is in C. It dates from 1978:
main() {
printf("hello, world\n");
}
Ten years later, the second version of the book updated this example for compatibility with ANSI C:
#include main() {
printf("hello, world\n");
}
But, nowadays, even this version needs an update; a modern C compiler will give an error when you try to compile this program:
C99 is more strict than ANSI C; it requires this:
#include int main() {
printf("hello, world\n");
}
There are three main parts to C’s version of Hello World:
- A call to the printf (“print formatted”) function, that does the main work. This function prints a string argument (text inside double quotes) to standard output. The \n at the end of the string represents a newline character.
- The #include statement at the beginning loads a library (stdio) which provides output (and input) functionality. This contains the actual code for the printf function. One curiosity of C is that it contains no built-in functions; you can write your own, but to call any others, you’ll need an appropriate #include statement.
- The printf line is inside a function called “main.” For C, this is a special function that will run automatically when you run the compiled program. Note that the most recent version of C requires you to declare an “int” (integer) return type for main, even if your function does not return a value.
The Hello World code is just one part of the puzzle, though. The remaining half is running the program. With a compiled language like C, you need to run a program to translate your source code into a standalone executable binary:
gcc hello.c
If all is well, the compiler will run silently and create an executable in your current directory named a.out. Run that program (./a.out), and you’ll see your code in action:
This is exactly how a correct Hello World program should behave, printing the text to your terminal, with a newline at the end, then terminating.
Hello World in Go
Go is a relatively modern language. It was released in 2009 and created by Google engineers, including Ken Thompson who invented the B language. Go has a simple syntax, much like C’s:
package mainimport "fmt"
func main() {
fmt.Println("hello, world")
}
You can run this program with go run hello.go or compile an executable using go compile hello.go and run it with ./hello.
Go’s package system will help you organize your code and reuse functionality according to solid engineering principles. For a simple program like Hello World, however, you just need to declare that it belongs to “package main” and ensure you have a main function to act as the default entry point.
Hello World in Rust
The Rust implementation of Hello World is so minimal, you might think it tells us nothing. But even this simple code contains some interesting nuances of the language.
fn main() {
println!("hello, world");
}
The call to println—which looks a lot like a function—includes an exclamation mark after its name. This means that println is actually a macro, not a function; you must call it using the exclamation mark syntax.

Related
Why You Should Learn Rust, Especially If You’re New to Programming
Rust is one of the newest programming languages, and it can change how you see code.
Like C, the main function acts as the entry point for this standalone program. Unlike C—and most other languages—Rust’s keyword to declare a function is very brief: fn. This is consistent with much of the language, which uses short keywords like “pub,” “mut,” and “ref.”
You can compile this program with rustc hello.rs, and run the generated executable with ./hello.
Hello World in Python
Python’s Hello World is notable as one of the most minimalist. This fits its reputation as a simple language, particularly well-suited to beginners. The entire program is:
print('hello, world')
Python is an interpreted language, so you can use the python command to run Python programs directly. Save the above code to a file (hello.py), run it (python hello.py), and you should see the familiar output.
Note that Python’s print() function adds a newline character by default, so you don’t need to include \n in your string. Also note that, unlike C, you can enclose a Python text string in either single quotes or double quotes.
Hello World in Java
Java is quite the opposite to Python. It’s often called a verbose language, and its Hello World program does nothing to dispel that:
public class HelloWorld {
public static void main(String[] args) {
System.out.print("hello, world\n");
}
}
In Java, even a simple standalone program must be represented by a class, and Java’s equivalent of C’s main() function has three modifying keywords:
- public is an access specifier which indicates the function is accessible throughout the program.
- static is a keyword that indicates this is a class method, not an instance method.
- void is a return type indicating that the function does not return a value.
You don’t need to know all the details behind these three keywords, but you will need to remember them to write any Java program.
It even takes quite a bit of typing to call Java’s print function. The “System.out.” before “print” identifies the standard output stream, which other languages use by default.
You’ll need to know one last Java quirk before you can get Hello World up and running: the file’s name. Because of Java’s class-first approach, the file you save your code in needs to be named after its public class: HelloWorld.java. Once you’ve created that file, run javac HelloWorld.java followed by java HelloWorld to tell the Java interpreter to run your program:
Every programming language is unique, but even across diverse languages such as the ones here, you can spot similarities. Most importantly, once you can run Hello World, you can focus on the specifics of the language you’re learning.