LearnFrenzy
Sign in Take a free test

Introduction to C · Lesson 2 of 35

Install a C Compiler and Write Hello World

4 min read Updated Nov 7, 2026 Saurabh Samir

To run C you need a compiler: a program that turns your source code into an executable file. This lesson sets one up on your computer, writes the traditional first program, and explains every line – the structure you'll reuse in every program from now on.

In this lesson

  • Installing GCC or Clang
  • Writing, compiling and running Hello World
  • The structure of a C program
  • Comments, statements and useful compiler flags

Install a compiler

SystemHow
WindowsInstall MSYS2 (msys2.org), then run pacman -S mingw-w64-ucrt-x86_64-gcc and add its bin folder to PATH. Or use WSL (Ubuntu inside Windows).
macOSRun xcode-select --install – it installs Clang (the gcc command also points to it).
Ubuntu / Debiansudo apt install build-essential
No installOnline compilers work for practice; install a real compiler as soon as you can.
gcc --version          # check the compiler is installed

For an editor, VS Code with Microsoft's "C/C++" extension gives highlighting, error squiggles and debugging. Code::Blocks and Dev-C++ are also common in colleges.

Hello, World!

Create a file named hello.c:

#include <stdio.h>

int main(void) {
    printf("Hello, World!\n");
    printf("Welcome to C programming.\n");
    return 0;
}

Output

Hello, World!
Welcome to C programming.

Compile and run it in a terminal:

gcc hello.c -o hello      # compile: creates the program "hello" (hello.exe on Windows)
./hello                   # run it (on Windows: hello)

Every line explained

The parts of a C program

  1. #include <stdio.h>preprocessor directive: brings in the standard input/output library, which declares printf
  2. int main(void)every program starts here; it returns an int to the operating system and takes no arguments
  3. { … }braces hold the function's body
  4. printf("…\n");prints text; \n is a newline; every statement ends with ;
  5. return 0;tells the operating system the program finished successfully

Common beginner errors

  • expected ';' before '}' – a missing semicolon on the line above.
  • implicit declaration of function 'printf' – you forgot #include <stdio.h>.
  • undefined reference to 'main' – misspelt main (C is case-sensitive: Main is different).
  • Output on the same line as the prompt – you forgot \n.

Comments and escape sequences

#include <stdio.h>

/* A multi-line comment:
   this program prints a small receipt. */
int main(void) {
    // A single-line comment (C99 and later)
    printf("Item\tQty\tPrice\n");             // \t = tab
    printf("Pen\t2\t20\n");
    printf("He said \"C is fast\"\n");       // \" = a double quote
    printf("Path: C:\\code\\hello.c\n");     // \\ = a backslash
    printf("100%% done\n");                  // %% = a percent sign in printf
    return 0;
}

Output

Item	Qty	Price
Pen	2	20
He said "C is fast"
Path: C:\code\hello.c
100% done

Compiler flags worth using

gcc -std=c17 -Wall -Wextra -pedantic hello.c -o hello
FlagMeaning
-std=c17use the C17 standard
-Wall -Wextraturn on most warnings – they catch real bugs
-pedanticwarn about non-standard code
-ginclude debug information (for gdb/lldb or VS Code debugging)
-O2optimise for speed
-lmlink the maths library (needed for sqrt, pow on Linux)

Treat warnings as errors

A C program that compiles with warnings often has a real bug. Every program in this course compiles with -Wall -Wextra -pedantic and zero warnings – aim for the same.

Practice

Try it yourself

  1. Print your name, college and branch on three lines, and a line with a tab between "Roll" and your roll number.

    Show solution
    #include <stdio.h>
    
    int main(void) {
        printf("Name: Asha Verma\n");
        printf("College: LearnFrenzy Institute\n");
        printf("Branch: CSE\n");
        printf("Roll\t21CS042\n");
        return 0;
    }

    Output

    Name: Asha Verma
    College: LearnFrenzy Institute
    Branch: CSE
    Roll	21CS042

Quick check

Where does every C program start running?

In the main function.

What does #include <stdio.h> do?

It includes the declarations of the standard I/O functions such as printf and scanf.

How do you print a % sign with printf?

Write %%.

Summary

  • Install GCC or Clang; compile with gcc file.c -o name and run ./name.
  • A program: #include lines, int main(void), statements ending in ;, return 0;.
  • Compile with -Wall -Wextra and fix every warning.

Finished this lesson?

Mark it complete to track your progress, then carry on with the next lesson.

Next lesson

Search LearnFrenzy

Esc

Quick links