Intermediate

Hello World in C

Your first C program - the classic Hello World example with Docker setup

Every programming journey starts with Hello World. This example is particularly special in C - it’s the original Hello World, first appearing in the 1978 K&R book “The C Programming Language.”

The Code

Create a file named hello.c:

1
2
3
4
5
6
#include <stdio.h>

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

Understanding the Code

  • #include <stdio.h> - Preprocessor directive that includes the standard I/O library, providing the printf function.
  • int main(void) - The entry point of every C program. int means it returns an integer, void means it takes no arguments.
  • printf("Hello, World!\n") - Prints the string to standard output. \n is the newline character.
  • return 0 - Returns 0 to the operating system, indicating successful execution.

Running with Docker

The easiest way to run this without installing a C compiler locally:

1
2
3
4
5
6
# Pull the official GCC image
docker pull gcc:14

# Compile and run the program
docker run --rm -v $(pwd):/app -w /app gcc:14 \
    sh -c "gcc -o hello hello.c && ./hello"

Running Locally

If you have GCC or Clang installed:

1
2
3
4
5
6
7
8
# Compile with GCC
gcc -o hello hello.c

# Or compile with Clang
clang -o hello hello.c

# Run the program
./hello

On Windows with MinGW or MSVC:

1
2
3
4
5
6
7
# MinGW
gcc -o hello.exe hello.c
hello.exe

# MSVC
cl hello.c
hello.exe

Expected Output

Hello, World!

Key Concepts

  1. C is compiled - Source code (.c) is compiled to machine code (executable binary)
  2. Preprocessing - #include directives are processed before compilation
  3. main() is special - Program execution begins at the main function
  4. Return values matter - Returning 0 signals success to the operating system
  5. No garbage collection - C requires manual memory management (not needed for this simple example)

A Note on History

The original K&R Hello World looked slightly different:

1
2
3
4
5
6
#include <stdio.h>

main()
{
    printf("hello, world\n");
}

Modern C requires explicit int return type and recommends void for empty parameters. The modern style shown in our main example follows the C99/C11/C17/C23 standards.

Next Steps

Continue to Variables and Data Types to learn about C’s type system and memory layout.

Running Today

All examples can be run using Docker:

docker pull gcc:14
Last updated: