Lesson 5 +10 XP

Your First C Program

Your First C Program

Time for a classic: "Hello, World!" in C.

The complete program

#include <stdio.h>

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

Line by line

  • #include <stdio.h> - brings in input and output functions like printf.
  • int main() - the entry point. Every program starts at main.
  • printf("Hello World!"); - prints text to the console.
  • return 0; - reports success to the operating system.
  • The curly braces { } mark the start and end of the function body.

Save and run it

Name the file hello.c, then:

gcc hello.c -o hello
./hello

Output:

Hello World!

Important rules

  • Always end with a semicolon ;.
  • File extension is .c.
  • The main function must return an int; most compilers want return 0; at least once.

TL;DR

  • #include <stdio.h> gets the print functions.
  • main() is where the program starts.
  • printf sends text to the console.
  • return 0; says the program ended successfully.