Your First Steps into C Programming
Welcome to C programming! If you're reading this, you're about to embark on an exciting journey into the world of programming. Don't worry if you've never used a command line before or if Linux sounds like a foreign language. By the end of this guide, you'll have written and run your very first C program.
Take a deep breath. We're going to go through this together, step by step.
Part 1: Setting Up Your Programming Environment
Before we can write C programs, we need to set up a proper environment. We'll be using something called WSL(Windows Subsystem for Linux), which lets you run Linux right inside your Windows computer. Think of it as having a tiny Linux computer living inside your Windows laptop.
Why Linux for C Programming?
C was born in the Unix/Linux world, and most professional C development happens on Linux systems. Learning to use Linux now will serve you well throughout your programming career. Plus, it's free!
Step 1: Check If You Already Have WSL
Let's first see if WSL is already installed on your computer.
Open PowerShell as Administrator
Click the Start button (the Windows icon in the bottom-left corner)
Type
PowerShellRight-click on "Windows PowerShell"
Select "Run as administrator"
If a box pops up asking "Do you want to allow this app to make changes?" click Yes
Check for WSL
In the blue PowerShell window, type the following command and press Enter:
wsl --list --verboseWhat you might see:
If WSL is installed with Ubuntu, you'll see something like:
NAME STATE VERSION Ubuntu Running 2Great news! Skip ahead to "Step 3: Starting Ubuntu"
If WSL is installed but no Ubuntu, you'll see a list but no Ubuntu. Continue to "Step 2B: Installing Ubuntu"
If you get an error or see a message about WSL not being recognized, you need to install WSL. Continue to the next section.
Step 2A: Enabling and Installing WSL (If Not Installed)
Still in your Administrator PowerShell window, type this command and press Enter:
wsl --install
This magic command does several things:
Enables the WSL feature in Windows
Downloads and installs the Linux kernel
Installs Ubuntu (a popular, beginner-friendly version of Linux)
This will take several minutes. You'll see progress messages. Be patient!
When it's done, you'll see a message asking you to restart your computer. Go ahead and restart.
Step 2B: Installing Ubuntu (If WSL Exists But No Ubuntu)
If you have WSL but no Ubuntu, run this command in Administrator PowerShell:
wsl --install -d Ubuntu
Wait for the download and installation to complete, then restart your computer if prompted.
Step 3: Starting Ubuntu and Creating Your User Account
After your computer restarts, Ubuntu might open automatically. If it doesn't:
Click the Start button
Type
UbuntuClick on the Ubuntu app
First-Time Setup: Creating Your User
The first time Ubuntu runs, it will ask you to create a user account. This is YOUR account for this Linux system.
You'll see a prompt like:
Enter new UNIX username:
Choose a username:
Use lowercase letters only
Keep it simple (like your first name:
alex,sam,jamie)No spaces allowed
Type your username and press Enter.
Next, you'll see:
New password:
Choose a password:
It can be simple for now (but don't use something like "password"!)
Important: When you type your password, nothing will appear on screen. No dots, no asterisks, nothing. This is normal! Linux is secretly recording what you type.
Type your password and press Enter
You'll be asked to retype it:
Retype new password:
Type the same password again and press Enter.
Congratulations! You now have your own Linux environment!
Step 4: Installing GCC (The C Compiler)
Now we need to install GCC (GNU Compiler Collection)—this is the tool that translates your C code into a program the computer can run.
First, Check If GCC Is Already Installed
In your Ubuntu window, type:
gcc --version
Press Enter.
If GCC is installed, you'll see something like:
gcc (Ubuntu 13.2.0-23ubuntu4) 13.2.0
Copyright (C) 2023 Free Software Foundation, Inc.
Skip ahead to Part 2: Understanding the Linux Terminal
If GCC is not installed, you'll see:
Command 'gcc' not found
That's okay! Let's install it.
Installing GCC
Type these commands one at a time, pressing Enter after each:
sudo apt update
You'll be asked for your password. Type it (remember, you won't see anything as you type) and press Enter.
This command updates the list of available software. You'll see lots of text scrolling by—that's normal.
Now install GCC:
sudo apt install gcc -y
The -y means "yes, install it without asking me to confirm."
Wait for the installation to complete. When you see your username and the $ symbol again, it's done!
Verify the installation:
gcc --version
You should now see the GCC version information. You're ready to program!
Part 2: Understanding the Linux Terminal
That black (or purple) window you're looking at is called the terminal. Instead of clicking on icons, you type commands to tell the computer what to do. It might seem old-fashioned, but it's incredibly powerful once you get the hang of it.
The Prompt: Your Command Line Friend
Look at your terminal. You'll see something like this:
alex@LAPTOP-ABC123:~$
Let's break this down:
alex— Your username@— Just a separator (means "at")LAPTOP-ABC123— Your computer's name:— Another separator~— Your current location (we'll explain this soon!)$— Indicates you're a regular user (ready for your command)
The blinking cursor after the $ is waiting for you to type a command.
Part 3: Navigating Your Linux World
Think of your files like a tree. At the base is the "root" (the starting point), and from there, folders branch out into more folders. Let's learn how to move around this tree.
Your Home Directory
When you start Ubuntu, you begin in your home directory. This is YOUR personal space where you'll keep your files and projects. The ~ symbol is a shortcut that represents your home directory.
Your home directory's full path is /home/yourusername (like /home/alex).
Let's Practice! Follow Along Carefully
From now on, I want you to type each command exactly as shown and observe what happens. This is how you'll learn!
Command 1: pwd (Print Working Directory)
Let's find out exactly where we are.
Type:
pwd
Press Enter.
You should see something like:
/home/alex
What just happened? pwd stands for "Print Working Directory." It tells you your current location in the file system. Right now, you're in your home directory!
Command 2: ls (List)
Let's see what's in this directory.
Type:
ls
Press Enter.
You might see nothing (if your home directory is empty) or you might see some default folders. Either is fine!
What just happened? ls lists all the files and folders in your current location.
Command 3: mkdir (Make Directory)
Let's create a folder for our C programming projects.
Type:
mkdir projects
Press Enter.
Nothing happened? Actually, something did! Linux usually stays quiet when things work. No news is good news!
Now type ls again:
ls
You should now see:
projects
What just happened? mkdir creates a new directory (folder). We just created a folder called projects.
Command 4: cd (Change Directory)
Let's go inside the projects folder.
Type:
cd projects
Press Enter.
Look at your prompt! It changed:
alex@LAPTOP-ABC123:~/projects$
See how ~ became ~/projects? That tells you you're now inside the projects folder within your home directory.
What just happened? cd stands for "Change Directory." It moves you into a different folder.
Command 5: pwd Again
Let's confirm our location:
pwd
You should see:
/home/alex/projects
You've moved!
Command 6: Create Another Folder
Let's create a folder for our first C program:
mkdir hello_world
Then list the contents:
ls
You should see:
hello_world
Move into this new folder:
cd hello_world
Check your location:
pwd
You should see:
/home/alex/projects/hello_world
You're now two levels deep: home → projects → hello_world
Command 7: cd .. (Go Up One Level)
What if you want to go back to the parent folder? Use two dots!
Type:
cd ..
Press Enter.
Now check:
pwd
You should see:
/home/alex/projects
What just happened? .. means "the parent directory" (one level up). So cd .. moves you up one folder.
Command 8: cd - (Go Back to Previous Location)
Here's a neat trick. Type:
cd -
Press Enter.
Check your location:
pwd
You're back in /home/alex/projects/hello_world!
What just happened? cd - takes you back to the previous directory you were in. It's like an "undo" for navigation!
Try it again:
cd -
Now you're back in /home/alex/projects. The - acts like a toggle between your last two locations.
Command 9: cd ~ (Go Home)
No matter where you are, you can always get home:
cd ~
Check:
pwd
You're back at:
/home/alex
What just happened? ~ always represents your home directory. cd ~ takes you home from anywhere!
Quick Navigation Summary
Let's review what you've learned. Practice each command:
| Command | What It Does |
pwd | Shows your current location |
ls | Lists files and folders here |
mkdir foldername | Creates a new folder |
cd foldername | Goes into a folder |
cd .. | Goes up one level (to parent folder) |
cd - | Goes back to previous location |
cd ~ | Goes to your home directory |
Part 4: Working with Files
Now let's learn about creating, copying, moving, and deleting files.
First, let's go to our hello_world folder:
cd ~/projects/hello_world
(See how we combined ~ with a path? You can do that!)
Command 10: nano (Text Editor)
nano is a simple text editor that runs in the terminal. Let's create a test file:
nano test.txt
A new screen appears! This is the nano editor.
Understanding Nano:
The top shows the filename
The main area is where you type
The bottom shows available commands (the
^means hold the Ctrl key)
Type this text:
Hello! This is my first text file.
I'm learning Linux!
Saving and Exiting Nano:
Press Ctrl + O (that's the letter O, not zero) to save
You'll see
File Name to Write: test.txtat the bottomPress Enter to confirm
Press Ctrl + X to exit nano
You're back at the command line!
Let's verify our file exists:
ls
You should see:
test.txt
To see what's inside the file without opening nano:
cat test.txt
You'll see your text displayed!
Command 11: cp (Copy)
Let's make a copy of our file:
cp test.txt backup.txt
List the files:
ls
You now have:
backup.txt test.txt
What just happened? cp source destination creates a copy of a file with a new name.
Command 12: mv (Move/Rename)
mv can move files to different locations OR rename them.
Let's rename our backup:
mv backup.txt mybackup.txt
List:
ls
You now have:
mybackup.txt test.txt
What just happened? mv oldname newname renames a file.
You can also move files to different folders:
mv mybackup.txt ~/
This moves mybackup.txt to your home directory.
Check your current folder:
ls
Only test.txt remains here.
Check your home folder:
ls ~
You'll see mybackup.txt there!
Command 13: rm (Remove)
Let's delete files we don't need. First, let's clean up that backup in home:
rm ~/mybackup.txt
Verify it's gone:
ls ~
Warning: rm permanently deletes files. There's no recycle bin! Be careful with this command.
Now remove the test file:
rm test.txt
Check:
ls
The folder is now empty.
Command 14: rmdir (Remove Directory)
Let's clean up by removing the empty hello_world folder and creating a fresh one.
First, go up one level:
cd ..
Now remove the empty folder:
rmdir hello_world
Check:
ls
It's gone!
Note: rmdir only works on empty directories. If there are files inside, you'd need rm -r foldername instead (the -r means recursive—it deletes everything inside too).
Part 5: Your First C Program!
This is the moment you've been waiting for! Let's write a real C program.
Setting Up
Create a fresh folder for our program:
mkdir hello_world
cd hello_world
Confirm you're in the right place:
pwd
You should see:
/home/alex/projects/hello_world
Writing the Program
Open nano to create your C program file:
nano hello.c
Important: C program files must end with .c
Now, carefully type the following program. Take your time and make sure every character is correct—computers are very picky!
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
Let's understand each line:
#include <stdio.h>— This tells C to include the "standard input/output" library, which gives us theprintffunctionint main() {— Every C program starts running frommain(). Theintmeans it returns a number when done. The{starts the function's code blockprintf("Hello, World!\n");— This prints text to the screen. The\ncreates a new linereturn 0;— This tells the operating system "I finished successfully" (0 means success)}— This ends the main function
Save and exit:
Press Ctrl + O
Press Enter to confirm the filename
Press Ctrl + X to exit
Compiling Your Program
Your C code is like a recipe—the computer can't run it directly. We need to compile it, which translates your human-readable code into instructions the computer understands.
Type:
gcc hello.c -o hello
Let's break this down:
gcc— The C compilerhello.c— Your source code file-o hello— Output (-o) the compiled program ashello
If you see no output, that's good! Silence means success.
If you see error messages, carefully compare your code to the example above. Common mistakes:
Missing semicolons (
;)Mismatched braces (
{and})Spelling errors
Let's see what was created:
ls
You should see:
hello hello.c
The hello file is your compiled program!
Running Your Program
The moment of truth! Type:
./hello
Why ./? The dot-slash tells Linux "run the program called hello that's in the current directory."
You should see:
Hello, World!
CONGRATULATIONS! You just wrote, compiled, and ran your first C program!
Let's Modify Your Program
The best way to learn is to experiment. Let's make changes!
Open your program:
nano hello.c
Modify it to look like this:
#include <stdio.h>
int main() {
printf("Hello, World!\n");
printf("I'm learning C programming!\n");
printf("This is so exciting!\n");
return 0;
}
Save (Ctrl + O, Enter) and exit (Ctrl + X).
Compile again:
gcc hello.c -o hello
Run again:
./hello
You should see:
Hello, World!
I'm learning C programming!
This is so exciting!
Every time you change your code, you need to compile it again before running!
Quick Reference Card
Here's everything you learned today:
Navigation
pwd # Where am I?
ls # What's here?
cd foldername # Go into folder
cd .. # Go up one level
cd - # Go to previous location
cd ~ # Go home
Files and Folders
mkdir foldername # Create folder
rmdir foldername # Delete empty folder
nano filename # Edit/create file
cat filename # Display file contents
cp source dest # Copy file
mv source dest # Move or rename file
rm filename # Delete file (careful!)
C Programming
nano program.c # Create C file
gcc program.c -o program # Compile
./program # Run
Nano Editor
Ctrl + O # Save
Ctrl + X # Exit
What's Next?
You've taken your first steps into the world of programming! Here are some things to try:
Experiment: Modify your hello.c program to print different messages
Explore: Navigate around your Linux system using
cdandlsPractice: Create more folders and files to get comfortable with the commands