# 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.

1. **Open PowerShell as Administrator**
    
    * Click the **Start** button (the Windows icon in the bottom-left corner)
        
    * Type `PowerShell`
        
    * Right-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**
        
2. **Check for WSL**
    
    In the blue PowerShell window, type the following command and press **Enter**:
    
    ```plaintext
    wsl --list --verbose
    ```
    
    **What you might see:**
    
    * **If WSL is installed with Ubuntu**, you'll see something like:
        
        ```plaintext
        NAME            STATE           VERSION
        Ubuntu          Running         2
        ```
        
        Great 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**:

```plaintext
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:

```plaintext
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:

1. Click the **Start** button
    
2. Type `Ubuntu`
    
3. Click 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:

```plaintext
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:

```plaintext
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:

```plaintext
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:

```bash
gcc --version
```

Press **Enter**.

**If GCC is installed**, you'll see something like:

```plaintext
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:

```plaintext
Command 'gcc' not found
```

That's okay! Let's install it.

### Installing GCC

Type these commands one at a time, pressing **Enter** after each:

```bash
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:

```bash
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:**

```bash
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:

```plaintext
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:

```bash
pwd
```

Press **Enter**.

You should see something like:

```plaintext
/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:

```bash
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:

```bash
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:

```bash
ls
```

You should now see:

```plaintext
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:

```bash
cd projects
```

Press **Enter**.

Look at your prompt! It changed:

```plaintext
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:

```bash
pwd
```

You should see:

```plaintext
/home/alex/projects
```

You've moved!

---

### Command 6: Create Another Folder

Let's create a folder for our first C program:

```bash
mkdir hello_world
```

Then list the contents:

```bash
ls
```

You should see:

```plaintext
hello_world
```

Move into this new folder:

```bash
cd hello_world
```

Check your location:

```bash
pwd
```

You should see:

```plaintext
/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:

```bash
cd ..
```

Press **Enter**.

Now check:

```bash
pwd
```

You should see:

```plaintext
/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:

```bash
cd -
```

Press **Enter**.

Check your location:

```bash
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:

```bash
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:

```bash
cd ~
```

Check:

```bash
pwd
```

You're back at:

```plaintext
/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:

```bash
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:

```bash
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:

```plaintext
Hello! This is my first text file.
I'm learning Linux!
```

**Saving and Exiting Nano:**

1. Press **Ctrl + O** (that's the letter O, not zero) to save
    
2. You'll see `File Name to Write: test.txt` at the bottom
    
3. Press **Enter** to confirm
    
4. Press **Ctrl + X** to exit nano
    

You're back at the command line!

Let's verify our file exists:

```bash
ls
```

You should see:

```plaintext
test.txt
```

To see what's inside the file without opening nano:

```bash
cat test.txt
```

You'll see your text displayed!

---

### Command 11: cp (Copy)

Let's make a copy of our file:

```bash
cp test.txt backup.txt
```

List the files:

```bash
ls
```

You now have:

```plaintext
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:

```bash
mv backup.txt mybackup.txt
```

List:

```bash
ls
```

You now have:

```plaintext
mybackup.txt  test.txt
```

**What just happened?** `mv oldname newname` renames a file.

You can also move files to different folders:

```bash
mv mybackup.txt ~/
```

This moves `mybackup.txt` to your home directory.

Check your current folder:

```bash
ls
```

Only `test.txt` remains here.

Check your home folder:

```bash
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:

```bash
rm ~/mybackup.txt
```

Verify it's gone:

```bash
ls ~
```

**Warning:** `rm` permanently deletes files. There's no recycle bin! Be careful with this command.

Now remove the test file:

```bash
rm test.txt
```

Check:

```bash
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:

```bash
cd ..
```

Now remove the empty folder:

```bash
rmdir hello_world
```

Check:

```bash
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:

```bash
mkdir hello_world
cd hello_world
```

Confirm you're in the right place:

```bash
pwd
```

You should see:

```plaintext
/home/alex/projects/hello_world
```

---

### Writing the Program

Open nano to create your C program file:

```bash
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!

```c
#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 the `printf` function
    
* `int main() {` — Every C program starts running from `main()`. The `int` means it returns a number when done. The `{` starts the function's code block
    
* `printf("Hello, World!\n");` — This prints text to the screen. The `\n` creates a new line
    
* `return 0;` — This tells the operating system "I finished successfully" (0 means success)
    
* `}` — This ends the main function
    

**Save and exit:**

1. Press **Ctrl + O**
    
2. Press **Enter** to confirm the filename
    
3. 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:

```bash
gcc hello.c -o hello
```

Let's break this down:

* `gcc` — The C compiler
    
* `hello.c` — Your source code file
    
* `-o hello` — Output (`-o`) the compiled program as `hello`
    

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:

```bash
ls
```

You should see:

```plaintext
hello  hello.c
```

The `hello` file is your compiled program!

---

### Running Your Program

The moment of truth! Type:

```bash
./hello
```

**Why** `./`? The dot-slash tells Linux "run the program called `hello` that's in the current directory."

You should see:

```plaintext
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:

```bash
nano hello.c
```

Modify it to look like this:

```c
#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:

```bash
gcc hello.c -o hello
```

Run again:

```bash
./hello
```

You should see:

```plaintext
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

```plaintext
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

```plaintext
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

```plaintext
nano program.c       # Create C file
gcc program.c -o program  # Compile
./program            # Run
```

### Nano Editor

```plaintext
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:

1. **Experiment:** Modify your hello.c program to print different messages
    
2. **Explore:** Navigate around your Linux system using `cd` and `ls`
    
3. **Practice:** Create more folders and files to get comfortable with the commands
