Skip to content

Hello World in C — Anatomy of a C Program and Compilation

DodaTech Updated 2026-06-28 7 min read

In this tutorial, you will learn about Hello World in C. We cover key concepts, practical examples, and best practices to help you master this topic.

The Hello World program in C is the simplest complete C program that prints text to the console, demonstrating the basic structure of every C application including headers, the main function, and output with printf.

Why It Matters

Hello World is not just tradition. It confirms that your development environment works correctly: compiler installed, linker configured, and runtime functional. Every concept you learn from this tiny program -- including header inclusion, function calls, return values, and string literals -- applies directly to programs of any size. Debugging a complex application starts with the same fundamentals that Hello World teaches.

Real-World Use

Every embedded system begins with a "blink" program that is the hardware equivalent of Hello World. Every server starts with a "Hello, World" HTTP response. When DodaTech's Durga Antivirus Pro initializes, it prints version information using the same printf-based formatting that Hello World demonstrates.

What You Will Learn

  • The complete anatomy of a C program from headers to return
  • How the main function works and why it returns an integer
  • Using printf to format and display output
  • The preprocessor's role in including stdio.h
  • Compilation stages and what each produces

Learning Path

flowchart LR
  A[What Is C] --> B[Installing Compiler]
  B --> C[Hello World
You are here] C --> D[Variables] D --> E[Control Flow] style C fill:#f90,color:#fff

The Hello World Program

Create a file named hello.c:

#include <stdio.h>

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

Compile and run:

gcc hello.c -o hello
./hello

Expected output:

Hello, World!

Breaking Down Every Piece

Let us examine each line of this program to understand what it does and why.

The Include Directive

#include <stdio.h>

This is a preprocessor directive. The preprocessor runs before compilation and literally copies the content of stdio.h into your source file. The angle brackets tell the preprocessor to search for the file in the system's standard include directory. stdio.h stands for "standard input/output header." It declares functions like printf, scanf, fopen, fgets, and many others.

Without this include, the compiler would not know the signature of printf and would generate a warning or error. You do not need to memorize everything in stdio.h, but you need to know it gives you access to console I/O functions.

The Main Function

int main() {

main is the entry point of every C program. When you run your executable, the operating system calls main. The int before it specifies that main returns an integer value to the operating system. The parentheses () indicate it takes no arguments (for now).

Why does main return an integer? Because the operating system expects a status code. Returning 0 means success. Any non-zero value indicates an error. This convention is universal across Unix and Windows.

Technically, main has two valid signatures:

int main(void)          // Explicitly no parameters
int main(int argc, char *argv[])  // Command-line arguments

Using void inside the parentheses is more explicit than empty parentheses, though both are accepted.

The Printf Function

printf("Hello, World!\n");

printf (print formatted) is declared in stdio.h. It takes a format string as its first argument and optionally additional values to format. The string "Hello, World!\n" is a string literal. The \n is an escape sequence representing a newline character (ASCII 10). It moves the cursor to the next line after printing.

The printf function is called with the parentheses. The semicolon at the end terminates the statement. In C, every statement must end with a semicolon. Forgetting it is one of the most common beginner mistakes.

The Return Statement

return 0;

return 0 sends the value 0 back to the operating system. It also exits the main function. The return statement can appear anywhere in a function, not just at the end. However, for main, reaching the closing brace } without a return statement implicitly returns 0 in C99 and later. It is good style to include return 0 explicitly.

Compilation in Detail

Let us trace what happens when you run gcc hello.c -o hello.

Preprocessing

The preprocessor expands #include <stdio.h> by inserting the entire content of the stdio.h header. It also removes comments and expands macros. You can see the preprocessed output:

gcc -E hello.c -o hello.i

The resulting hello.i file is enormous because stdio.h contains hundreds of lines of declarations and type definitions.

Compilation

The compiler translates the preprocessed C code into assembly language specific to your processor architecture:

gcc -S hello.i -o hello.s

This produces hello.s containing assembly instructions like movl, call, and ret. You can read it even if you do not know assembly -- the structure is usually recognizable with labels like main:.

Assembly

The assembler converts assembly into machine code in an object file:

gcc -c hello.s -o hello.o

The hello.o file is binary machine code but not yet executable. It has an unresolved reference to printf. The linker must resolve this.

Linking

The linker finds the printf implementation in the C standard library (libc) and links it into the final executable:

gcc hello.o -o hello

The result is a fully executable binary. On Linux, you can see the dynamically linked libraries:

ldd hello

Expected output includes libc.so -- the C standard library that contains printf.

The Main Function in Detail

The main function is special in C. Unlike other functions, you do not call main yourself -- the operating system calls it. When main returns, the Process exits with that return code.

Command-Line Arguments

Here is a more complete version that accepts arguments:

#include <stdio.h>

int main(int argc, char *argv[]) {
    printf("Program name: %s\n", argv[0]);
    for (int i = 1; i < argc; i++) {
        printf("Argument %d: %s\n", i, argv[i]);
    }
    return 0;
}

Compile and run with arguments:

gcc args.c -o args
./args hello world test

Expected output:

Program name: ./args
Argument 1: hello
Argument 2: world
Argument 3: test

argc is the argument count. argv is the argument vector -- an array of C strings. argv[0] is always the program name. The arguments start at argv[1].

Common Mistakes

1. Forgetting the Semicolon

Every statement must end with ;. The compiler error "expected ';'" is the most common beginner error.

2. Using Wrong Quotes

C uses double quotes for strings: "hello". Single quotes are for characters: 'h'. Using 'hello' is invalid.

3. Forgetting the Newline

printf("Hello") without \n prints the text but the next shell prompt appears on the same line. The \n flushes the output buffer and moves to the next line.

4. Misspelling main

The function must be spelled main, not Main, mian, or maine. The operating system specifically looks for main.

5. Using Void Main

Some tutorials use void main(). This is incorrect in standard C. The standard requires int main(void) or int main(int argc, char *argv[]).

Practice Questions

  1. What does #include <stdio.h> do? It includes the standard input-output header, which declares printf, scanf, and other I/O functions.

  2. Why does main return an integer? The integer is a status code returned to the operating system. 0 means success; non-zero means error.

  3. What does the \n escape sequence mean? It inserts a newline character, moving the cursor to the next line.

  4. What are the four stages of compilation? Preprocessing, compilation (to assembly), assembly (to machine code), and linking.

  5. Challenge: Write a program that prints your name, age, and favorite programming language on three separate lines using a single printf call.

Mini Project: Personal Greeting

Write a program that accepts a name as a command-line argument and prints a personalized greeting:

#include <stdio.h>

int main(int argc, char *argv[]) {
    if (argc < 2) {
        printf("Usage: %s <name>\n", argv[0]);
        return 1;
    }
    printf("Hello, %s! Welcome to C programming.\n", argv[1]);
    return 0;
}

FAQ

Can I write Hello World without printf?

Yes, you can use puts() which adds a newline automatically: puts('Hello, World!'). Or use write() for low-level output.

What happens if I omit the return 0?

In C99 and later, reaching the end of main() implicitly returns 0. It is still good practice to include it explicitly.

Why is the file called .c and not .cprog or something else?

By convention, .c stands for C source code. The compiler recognizes .c files as C source. Header files use .h.

What does int main(void) mean vs int main()?

In C, int main(void) explicitly says main takes no parameters. int main() leaves the parameter list unspecified (older C style). Prefer int main(void).

Can I compile without linking to libc?

Yes, for freestanding environments (embedded systems, OS kernels). You provide your own startup code and cannot use printf or malloc.

What is Next

Now that you understand the basic program structure, proceed to Variables in C to learn about integer, floating-point, and character types, and how to store and manipulate data in memory.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro

Home Browse C