Lesson 1: Getting Started with C – “Hello, World!” and Beyond


Lesson 1

Welcome to the Wonderful (and Occasionally Infuriating) World of C!

C is a bit like coffee: if you start your day with it, you’re wide awake and full of energy. But have too much, and things start going wrong very quickly. So, buckle up, and let’s make sure we don’t end up with a code-induced headache!

In this lesson, we’ll take our first steps by writing the classic “Hello, World!” program. Think of it as introducing yourself to C, so it knows you’re friendly (and not here to crash the system… yet).


Lesson Goals

  1. Set up a development environment.
  2. Install GCC compiler on different Linux distributions.
  3. Write and run your first C program.
  4. Understand the basic structure of a C program.
  5. Compile your code using gcc without pulling out your hair.

Step 1: Installing GCC on Your Linux Distribution

Before we dive into coding, let’s make sure you have the GCC compiler installed. Without GCC, trying to compile C code is like trying to write an essay without a keyboard—it’s just not happening.

Follow the instructions below for your Linux flavor:

For Arch Linux (because you like a challenge):

  1. Update the system (It’s a good habit to always do this on Arch):

    sudo pacman -Syu
    
  2. Install GCC:

    sudo pacman -S gcc
    
  3. Verify the Installation: Run this command to check if GCC is installed correctly:

    gcc --version
    

For Fedora (for the practical and curious):

  1. Update the system:

    sudo dnf update
    
  2. Install GCC:

    sudo dnf install gcc
    
  3. Verify the Installation: Check the GCC version to confirm everything’s fine:

    gcc --version
    

For Ubuntu / Debian (stability first, always):

  1. Update the system:

    sudo apt update
    
  2. Install GCC:

    sudo apt install gcc
    
  3. Verify the Installation: Run the version command:

    gcc --version
    

If the version number shows up, you’re good to go!


Step 2: Writing the Famous “Hello, World!” Program

If Shakespeare wrote code, “Hello, World!” would be his “To be, or not to be”. Let’s create our first program:

  1. Create a New File:
    Open your favorite text editor (Vim, Nano, or that obscure one you found last week) and create a new file named hello.c:

    nano hello.c
    
  2. Type the Following Code:
    Enter the magical lines of code below:

    #include <stdio.h>
    
    int main() {
        printf("Hello, World!\n");
        return 0;
    }
    

    Let’s break it down:

    • #include <stdio.h>: This tells C to include the Standard Input Output library. Think of it as importing a box of tools you’ll use to display text on the screen.
    • int main() { ... }: The main() function is the starting point of every C program. It’s like the captain of a ship; without it, the ship (program) just sits there.
    • printf("Hello, World!\n");: This line prints “Hello, World!” on the screen. The \n at the end adds a new line because we don’t want everything jammed together.
    • return 0;: This tells your program to exit successfully. If your code misbehaves, returning something other than zero is like saying, “Oops, I broke it!”
  3. Save and Exit:

    • In Nano, hit Ctrl + X to exit, Y to save, and Enter to confirm.
    • In Vim, if you’re trapped, try :wq and hope it works. If not, just reboot the computer (kidding… sort of).

Step 3: Compiling Your C Program

Now that we have the code, let’s see if we can get the computer to actually run it. This step is where most C beginners have their first facepalm moment.

  1. Compile Using GCC:
    In your terminal, run:

    gcc hello.c -o hello
    

    Let’s decode this command:

    • gcc hello.c: This tells GCC (GNU Compiler Collection) to compile the hello.c file.
    • -o hello: This tells the compiler to name the output file hello (instead of the default a.out, which sounds more like an error message).
  2. Run the Compiled Program:
    Type the following command:

    ./hello
    

    If everything went well, you should see:

    Hello, World!
    

    If not, check for missing semicolons. C programmers have a love-hate relationship with those tiny ; characters. Forget one, and the compiler will be quick to remind you… in the form of 20 lines of cryptic error messages.


Step 4: Analyzing the Program Structure

Let’s take a closer look at the structure of our code:

  1. Header Files (#include <stdio.h>):
    Think of these as special instruction manuals. If you want to use certain functions (like printf), you need to tell the program where to find them.

  2. The main() Function:
    This is the starting point of your program. Without it, C doesn’t know where to begin. It’s like turning on a GPS without setting a destination.

  3. Curly Braces {}:
    These are used to group code blocks. If you forget one, your code will behave unpredictably, just like missing the ending bracket in an IKEA manual.

  4. printf() Function:
    This function outputs text to the console. We’ll be using it a lot, so get comfy!

  5. return 0;:
    This ends the program and tells the operating system, “Everything went fine!” Use it wisely, or the OS will think your program crashed.


Homework

  1. Modify the program to print your name, favorite programming language, and a fun fact about yourself.
  2. Create a second file named goodbye.c that prints “Goodbye, World!” instead.

Bonus Tips

  • Tip #1: If the compiler yells at you, don’t take it personally. It’s like a strict teacher who only wants to see perfect code.
  • Tip #2: Comment your code! Use // for single-line comments and /* ... */ for multi-line comments. Commenting is like leaving sticky notes for your future self.
  • Tip #3: Never skip return 0; – it’s like forgetting to say goodbye after a long phone call.

Conclusion

Congratulations! You’ve just written and run your first C program. Today, it’s “Hello, World!”—tomorrow, it’s conquering low-level memory management and dominating pointers (don’t worry, we’ll get there).

For now, give yourself a pat on the back, and let me know when you’re ready to tackle Lesson 2!