Stat Mania
  • C Programming
  • All Courses
  1. Getting Started
  2. C's "Hello, World!" Explained Line by Line (for R & Python Users)
  • Course Home
  • Getting Started
    • C's "Hello, World!" Explained Line by Line (for R & Python Users)
    • How to Test a Small C Snippet Quickly
  • Variables and Data Types
    • Declaring Multiple Variables in One Line in C
    • float vs double in C: Precision, Memory, and When to Use Which
  • Operators and Input
    • Pre-Increment vs Post-Increment in C: ++i vs i++
    • Checking User Input in C: Is It Really a Number?
  • Making Decisions
    • Nested if in C, Explained with Real-Life Examples
    • Shortcut Ways to Write if in C: the Ternary Operator and More
    • The switch Statement in C: Syntax, break, Fall-Through and Examples
  • Loops
    • The do-while Loop in C: Run First, Check After
    • Why Use break in a while Loop If the Condition Already Stops It?
    • Printing a Comma-Separated List in C (No Trailing Comma, Clean Line Breaks)
    • 5 while Loop Practice Problems in C (with Hints and Solutions)
    • 5 for Loop Practice Problems in C (with Hints and Solutions)
    • 5 Nested for Loop Practice Problems in C (with Hints and Solutions)
  • Arrays and Structs
    • How to Save Loop Results in an Array in C
    • Structs in C: Grouping Related Data (with Python and R Comparisons)
  • Random Numbers
    • srand(time(NULL)) in C: Why Random Numbers Repeat and How to Seed Them
    • Tossing a Die in C Until We Get a 6

On this page

  • Line 1: #include <stdio.h>
  • Line 3: int main() {
  • Line 5: printf("Hello, World!\n");
  • Line 7: return 0;
  • Line 8: }
  • The Big Picture: What happens when you run this?
  • Why this feels so different from R/Python
  1. Getting Started
  2. C's "Hello, World!" Explained Line by Line (for R & Python Users)

C’s “Hello, World!” Explained Line by Line (for R & Python Users)

A line-by-line breakdown of the classic C Hello World program — what each part does, why C requires it, and how it compares to Python and R.
Reading 5 min

By the end of this lesson you will

  • Explain every line of the classic Hello, World! program
  • Compare C’s structure with Python and R

Resources

  • printf reference
  • GCC manual

The classic “Hello, World!” program in C is as follows:

#include <stdio.h>

int main() {

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

  return 0;
}

Let’s break it down line by line, explaining not just what each line does, but why it’s there.


Line 1: #include <stdio.h>

What it does: This is a preprocessor directive. Before the C compiler actually translates your code into machine language, a program called the preprocessor scans your code. When it sees #include, it literally copies and pastes the contents of another file into your code at that exact spot.

Here, it’s copying the contents of a file called stdio.h (Standard Input/Output header) into your program.

Why it’s needed: C is a very small language. It doesn’t even have built-in commands to print text to the screen or read input from the keyboard. Those capabilities live in external libraries.

printf (which we use on line 5) is not a built-in C keyword. It is a function defined in the standard library. The stdio.h file contains the function prototype (a declaration) for printf. Without this include, the compiler wouldn’t know that printf exists, what arguments it takes, or what it returns. It would throw an error or a warning.

Analogy for R/Python users: Think of it like import in Python or library() in R. In Python, if you want to use sqrt(), you have to import math first. In C, if you want to use printf(), you have to #include <stdio.h>.


Line 3: int main() {

What it does: This defines the main function.

  • int means this function will return an integer value to the operating system when it finishes.
  • main is the special name C looks for. It is the entry point of every C program. When you run the compiled program, execution always starts at the first line inside main.
  • () indicates this function takes no parameters (for now).
  • { marks the beginning of the function’s body.

Why it’s needed: Unlike Python or R, where you can write code at the top level of a script and it just runs from top to bottom, C requires a structured entry point. The operating system needs to know exactly where to start executing your instructions. main is that starting line.

Analogy for R/Python users: In Python, you often see:

if __name__ == "__main__":
    # code starts here

C’s main() function is essentially that concept, but it is mandatory. You cannot write a C program without a main function.


Line 5: printf("Hello, World!\n");

What it does: This is the actual instruction. It calls the printf function (Print Formatted) and passes it a single argument: the string "Hello, World!\n".

  • printf takes the string and sends it to the standard output (usually your terminal/console).
  • \n is an escape sequence. It represents a newline character. It tells the terminal to move the cursor to the next line after printing. Without it, your terminal prompt would appear immediately after the exclamation mark on the same line.
  • The ; (semicolon) at the end is mandatory in C. It marks the end of a statement. Unlike Python (which uses newlines) or R (which can use newlines or semicolons), C requires a semicolon after every single executable statement. Forgetting it is the #1 beginner error.

Why it’s needed: This is the actual work of the program. Everything else is setup and structure.

Analogy for R/Python users: * Python: print("Hello, World!") * R: cat("Hello, World!\n") or print("Hello, World!") * The \n is the same concept in all three languages, but in C, it is almost always required if you want clean output.


Line 7: return 0;

What it does: This ends the main function and sends the integer value 0 back to the operating system.

Why it’s needed: Remember how main was declared as int main()? That means it must return an integer.

In C and Unix/Linux conventions, a return value of 0 means success. Any non-zero value (like 1, -1, or EXIT_FAILURE) indicates that an error occurred during execution.

When you run a program in a terminal and then type echo $? (on Linux/Mac) or echo %ERRORLEVEL% (on Windows), you will see the value returned by main. Scripts and automated tools use this exit code to determine if your program ran correctly.

Analogy for R/Python users: * Python: sys.exit(0) explicitly exits with a success code. If you just let a script end, Python implicitly returns 0. * R: quit(status = 0) does the same thing. * In C, you must explicitly write return 0; (or return EXIT_SUCCESS;) at the end of main to signal success.


Line 8: }

What it does: This closing curly brace marks the end of the main function’s body.


The Big Picture: What happens when you run this?

  1. Preprocessing: The preprocessor sees #include <stdio.h> and pastes the contents of that header file into your code.
  2. Compilation: The compiler translates your C code into assembly language, then into machine code (object files).
  3. Linking: The linker combines your object file with the standard C library (which contains the actual compiled code for printf).
  4. Execution: You run the executable. The OS loads it into memory, finds the main function, and starts executing line by line.
  5. Output: printf sends “Hello, World!” followed by a newline to your terminal.
  6. Exit: return 0; tells the OS the program finished successfully.

Why this feels so different from R/Python

In R and Python, the interpreter handles memory, types, and execution flow for you. In C, you are explicitly telling the computer:

  1. What external code to bring in (#include)
  2. Where the program starts (main)
  3. What the program does (printf)
  4. How to cleanly exit (return 0)

It is more verbose, but it gives you complete control over the machine. As you progress, you will learn to appreciate this structure, especially when you start managing memory and building complex data structures.

  • Every C program starts running at main.
  • #include <stdio.h> makes printf available.
  • return 0; tells the system the program succeeded.

Compiler. A program that turns C source code into a program the computer can run, such as gcc.

Back to top
Stat Mania

Free, hands-on statistics and programming courses that run right in your browser.

Courses

All courses R Programming Statistics Probability

Resources

Practice quizzes Tools & games Quarto docs Course kit on GitHub

Company

Home About Contact Privacy policy Terms of use
© Stat Mania. All rights reserved.
Getting Started
How to Test a Small C Snippet Quickly