Hello World in C — Your First C Program Explained (with printf)

Every C programmer starts with the same five lines. Let's write them together — and actually understand what each one does before you move on.

A C Hello World program in a code editor with Hello, World! shown in the terminal

Every C programmer’s journey begins with the same few lines: the “Hello, World!” program, which simply displays the text “Hello, World!” on the screen. It’s the traditional first program in any language — a quick way to confirm your setup works and to introduce the basic structure of a program. It’s also the starting point of our C programming tutorials series, and if you’re curious how it looks elsewhere, see our list of Hello World programs in 300 programming languages.

Why trust this guide

Every code example here compiles and runs as shown.

Table of Contents

This guide walks through the C version line by line — explaining what each part does, how to compile and run it on Windows, Linux and macOS, the errors beginners hit most often, and a few variations to try next. Let’s take a look.

The Complete Program

#include <stdio.h>

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

When you compile and run this program, it prints:

Hello, World!

Line-by-Line Explanation

  • #include <stdio.h> — This line tells the compiler to include the standard input/output library, which contains printf(). Without it, the program can’t print anything. Lines beginning with # are handled by the preprocessor before compilation.
  • int main(void) — Every C program starts running from main() function. The int means it returns an integer to the operating system; void means it takes no input arguments.
  • printf("Hello, World!\n");printf (“print formatted”) is the standard library function that writes text to the screen. The \n is an escape sequence that adds a newline so the next output starts on a fresh line. The semicolon ends the statement.
  • return 0; — Returns 0 to the operating system, signalling that the program finished successfully. A non-zero value would indicate an error.
Diagram labeling the parts of a C Hello World program: include, main, printf and return

Understanding the printf Function

Because printf is the first function most C beginners meet, it’s worth understanding a little more. printf can do far more than print fixed text — it can format and insert values using format specifiers:

#include <stdio.h>

int main(void)
{
    int year = 2026;
    printf("Hello, World! It is %d.\n", year);
    return 0;
}

Here %d is replaced by the value of year. Common specifiers include %d (integer), %f (float), %c (character) and %s (string). You’ll use printf in almost every C program you write.

How to Compile and Run

Save your code in a file called hello.c, then compile and run it for your system:

SystemCompileRun
Linux / macOS (GCC/Clang)gcc hello.c -o hello./hello
Windows (MinGW GCC)gcc hello.c -o hello.exehello.exe
Windows (Turbo C)Press Ctrl+F9 in the IDEOutput shows in the console

If you don’t have a compiler yet, see our guide to the best C and C++ compilers or how to set up Turbo C on Windows 10/11.

Common Errors Beginners Hit

Error messageCauseFix
undefined reference to 'printf'Missing #include <stdio.h>Add the include line at the top
expected ';' before...Missing semicolonEnd the statement with ;
'printf' was not declaredTypo (e.g. Printf, print)C is case-sensitive — use lowercase printf
Output runs into the next lineMissing \nAdd \n inside the quotes

Variations to Try

Once the basic program works, try these small modifications to build confidence:

// Ask for the user's name, then greet them
#include <stdio.h>

int main(void)
{
    char name[50];
    printf("What is your name? ");
    scanf("%49s", name);
    printf("Hello, %s!\n", name);
    return 0;
}
// Print Hello, World! five times with a loop
#include <stdio.h>

int main(void)
{
    for (int i = 0; i < 5; i++)
        printf("Hello, World!\n");
    return 0;
}

printf vs puts

You can also print text with puts(), which automatically adds a newline:

#include <stdio.h>

int main(void)
{
    puts("Hello, World!");
    return 0;
}

Use puts() for simple lines of text and printf() when you need formatting or to insert values. (Note: always include return 0; and use int main(void) — the earlier int main(int argc, char** argv) form is for programs that read command-line arguments, which you don’t need yet.)

FAQ

Next Steps

Now that your first program runs, continue with:

  1. Variables/Data Types in C
  2. Operators in C
  3. Control Flow in C
  4. Functions in C
Scroll to Top