🛠️ Lesson 2: Setup & Your First Program
⏱️ Estimated time: 20 minutes | Difficulty: Beginner
What You'll Learn
How to set up a C compiler, use an IDE, and understand the structure of a basic C program.
Setting Up Your C Environment
Unlike Python, C is a compiled language. You need a compiler to convert your C code into an executable program your computer can run.
Step 1: Install a Compiler
🪟 Windows
- Download MinGW-w64
- Install and add to PATH
- Or use Visual Studio (MSVC compiler)
🐧 Linux
sudo apt-get install gcc
GCC comes pre-installed on most Linux distros!
🍎 macOS
xcode-select --install
Installs Clang compiler (compatible with GCC)
Step 2: Verify Installation
gcc --version
# Should show something like: gcc (GCC) 13.2.0
Step 3: Choose a Code Editor
- 📝 VS Code (Recommended) — Free, extensions for C
- 📝 Code::Blocks — Built-in compiler and debugger
- 📝 Notepad++ — Lightweight text editor
Your First C Program
Create a file called hello.c and type this:
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
Let's Break It Down Line by Line:
#include <stdio.h>— Includes the Standard Input/Output library (gives usprintf)int main()— The main function where your program starts. Every C program must have one!printf("Hello, World!\n")— Prints text to the screen.\nmeans new linereturn 0;— Tells the operating system "I finished successfully"{ }— Curly braces define a block of code
Compiling & Running
# Step 1: Compile
gcc hello.c -o hello
# Step 2: Run
./hello # Linux/Mac
hello.exe # Windows
# Output: Hello, World!
⚠️ Common Mistake: Forgetting the semicolon ; at the end of statements! C requires
semicolons after every statement. If you get an error, check for missing semicolons first.
More Examples
#include <stdio.h>
int main() {
printf("My name is GK Solutions\n");
printf("I am learning C programming!\n");
printf("2 + 3 = %d\n", 2 + 3); // %d prints an integer
return 0;
}