Introduction to C · Lesson 2 of 35
Install a C Compiler and Write Hello World
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
| System | How |
|---|---|
| Windows | Install 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). |
| macOS | Run xcode-select --install – it installs Clang (the gcc command also points to it). |
| Ubuntu / Debian | sudo apt install build-essential |
| No install | Online 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;
}
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
- #include <stdio.h>preprocessor directive: brings in the standard input/output library, which declares printf
- int main(void)every program starts here; it returns an int to the operating system and takes no arguments
- { … }braces hold the function's body
- printf("…\n");prints text; \n is a newline; every statement ends with ;
- 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:Mainis 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;
}
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
| Flag | Meaning |
|---|---|
-std=c17 | use the C17 standard |
-Wall -Wextra | turn on most warnings – they catch real bugs |
-pedantic | warn about non-standard code |
-g | include debug information (for gdb/lldb or VS Code debugging) |
-O2 | optimise for speed |
-lm | link 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
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; }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 nameand run./name. - A program: #include lines,
int main(void), statements ending in;,return 0;. - Compile with
-Wall -Wextraand fix every warning.
Finished this lesson?
Mark it complete to track your progress, then carry on with the next lesson.