C Hello World: Write, Compile and Run Your First Program

C Hello World: Write, Compile and Run Your First Program

A school office wants a program that prints a welcome message. Before reading marks or making decisions, we need to connect a saved source file with a running program.

A C program describes operations in source code. A compiler translates that source into an executable; running the executable performs the operations. In a hosted program, main is the starting function.

  1. Explain

    Explain the roles of the source file, compiler and running program.

  2. Trace

    Replace the school name with Hill School while preserving the newline.

  3. Apply

    Distinguish saving a source file from building and running it.

Hello World: follow the data

  1. Start

    A school office wants a program that prints a welcome message. Before reading marks or making decisions, we need to connect a saved source file with a running program.

  2. Operation

    A C program describes operations in source code. A compiler translates that source into an executable; running the executable performs the operations. In a hosted program, main is the starting function.

  3. Result

    Welcome to River School

A guided model of this example. The illustration explains the process; run the complete C program to check its output.

Understand the operation

Save the code as school.c. The stdio.h header declares printf, the standard output function. int main(void) says that the starting function returns an int and receives no parameters. Braces enclose its body; the semicolon ends a statement.

The text inside double quotes is a string literal. The escape sequence \n requests a newline; it is not two printed characters. return 0 reports successful termination to the environment. These statements do not run merely because a file was saved.

Read the complete C program

This complete C17 program uses a small school example. Read declarations before the statements that use their values. Each printf format describes how the corresponding value is written to standard output.

C17 · complete program

#include <stdio.h>
int main(void) {
  printf("Welcome to River School\n");
  return 0;
}

Expected output

Welcome to River School
Run in C compiler →

Follow the values through the code

C17 · code used in this explanation

#include <stdio.h>
int main(void) {
  printf("Welcome to River School\n");
  return 0;
}

The compiler checks the source and links the required library support into a runnable program.

When you run it, main calls printf and then returns zero. The text goes to standard output, usually your terminal.

Follow the worked calculation

  1. Read the starting values

    The compiler checks the source and links the required library support into a runnable program.

  2. Perform the operation

    When you run it, main calls printf and then returns zero. The text goes to standard output, usually your terminal.

  3. Check the observable result

    Welcome to River School

    Welcome to River School
A guided model of this example. The illustration explains the process; run the complete C program to check its output.

Compile the saved file, then run the executable

For a local GCC or Clang toolchain, use the terminal commands below from the source file’s folder. -std=c17 selects the language version; warning flags help expose questionable code. Install a compiler appropriate to your operating system first.

Unix-like shell commands:

cc -std=c17 -Wall -Wextra -Wpedantic school.c -o school
./school

The compiler command finishes before you run ./school. On Windows, use the executable name and command syntax provided by your installed toolchain. A missing compiler is a setup problem, while a missing semicolon is a source error.

Try a small variation

C17 · code used in this explanation

#include <stdio.h>
int main(void) {
  printf("Welcome to River School\n");
  return 0;
}

Replace the school name with Hill School while preserving the newline.

Worked answer

Only the string content changes. The function call, parentheses, semicolon and newline remain, so the output is one line with the new school name.

C17 · complete program

#include <stdio.h>
int main(void) {
  printf("Welcome to Hill School\n");
  return 0;
}

Expected output

Welcome to Hill School
Run in C compiler →

Recognize the important boundaries

C is case-sensitive: Main is not the standard starting name main. Most expression statements need a semicolon, but a function definition’s closing brace does not require one.

Use the C online compiler or a local C compiler. Do not use void main as a portable replacement for int main(void).

Check your understanding

Choose the answer, then compare the reasoning for every option.

What runs when you save school.c?
Choose one answer. You can try again.

Answer and explanation

Nothing runs until you invoke the compiled program.

Correct. Saving creates source text; compilation and execution are separate actions.

Use the idea independently

A C program describes operations in source code. A compiler translates that source into an executable; running the executable performs the operations. In a hosted program, main is the starting function.

Continue with compiler messages and local setup.

Continue with a second greeting program.

Language reference: WG14 C11 committee draft. These examples use the C17-compatible subset of the explained language features.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Compile C and Understand Errors Before Running →