C - Hello World

Your First C Program

Every C program starts at main. Let’s print a message and learn how to compile/run from the terminal.

Learning Objectives

  • Write, compile, and run a simple C program.
  • Understand the role of #include and main.

Prerequisites

Code

// hello.c
#include <stdio.h>

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

Compile and Run

gcc -std=c11 -Wall -Wextra -O0 -g hello.c -o hello
./hello
# Output: Hello World!

Common Pitfalls

  • Forgetting a newline (\n) at the end of the printed line.
  • Missing semicolons after statements.

Checks for Understanding

  1. What header provides printf?
  2. What is the return type of main?
Show answers
  1. <stdio.h>
  2. int

Practice

  1. Modify the program to print your name on a new line.
  2. Print the result of 2 + 3 using printf.