Your First C Lab

CCBeginner
Practice Now

Introduction

Welcome to LabEx! This is your first C lab, where you'll take your initial steps into the world of C programming. Don't worry if you're completely new to this - we'll guide you through every step.

In this lab, you'll learn how to:

  1. Write and run your first C program
  2. Use basic output in C
  3. Work with a simple variable

These fundamental skills will start your journey into C programming. Let's begin!

Click the Continue button below to begin the lab.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL c(("`C`")) -.-> c/UserInteractionGroup(["`User Interaction`"]) c(("`C`")) -.-> c/BasicsGroup(["`Basics`"]) c(("`C`")) -.-> c/CompoundTypesGroup(["`Compound Types`"]) c/UserInteractionGroup -.-> c/output("`Output`") c/BasicsGroup -.-> c/comments("`Comments`") c/BasicsGroup -.-> c/variables("`Variables`") c/BasicsGroup -.-> c/data_types("`Data Types`") c/CompoundTypesGroup -.-> c/strings("`Strings`") subgraph Lab Skills c/output -.-> lab-391824{{"`Your First C Lab`"}} c/comments -.-> lab-391824{{"`Your First C Lab`"}} c/variables -.-> lab-391824{{"`Your First C Lab`"}} c/data_types -.-> lab-391824{{"`Your First C Lab`"}} c/strings -.-> lab-391824{{"`Your First C Lab`"}} end

Your First C Program

Let's start with the traditional first program for any programmer - "Hello, World!". This simple program will help you understand the basic structure of a C program.

First, we need to open our WebIDE. Look for the "WebIDE" button on your LabEx interface and click it. This will open a VS Code-like environment where we'll write our C code.

Once the WebIDE is open, follow these steps:

  1. In the left sidebar, you'll see a file explorer. Right-click in this area and select "New File".
  2. Name the file hello.c. The .c extension tells the IDE that this is a C file.
  3. Click on the new hello.c file to open it in the editor.
alt text

Now, let's write our first C program. Copy and paste the following code into your hello.c file:

#include <stdio.h>

int main() {
    printf("Hello, World!\n");
    return 0;
}

Let's break down this code:

  • #include <stdio.h>: This line tells the computer to use a pre-written code that allows us to input and output information. Think of it like importing a tool we need to use.
  • int main() { ... }: This is where our program starts. Everything inside the curly braces { } is what our program will do. Every C program needs a main function.
  • printf("Hello, World!\n");: This line tells the computer to print "Hello, World!" to the screen.
    • printf is a function that sends text to the screen.
    • The \n at the end of the text moves to a new line after printing, like pressing Enter on your keyboard.
  • return 0;: This tells the computer our program has finished successfully. It's like saying "We're done here!"

To run this program:

  1. Open the terminal in your WebIDE (usually at the bottom of the screen).
  2. In the terminal, type the following commands:
alt text
gcc hello.c -o hello
./hello

The first command (gcc hello.c -o hello) prepares your program to run. It's called "compiling" and it translates your C code into a language the computer can understand. The -o hello part names the resulting program "hello".

The second command (./hello) actually runs your program. The ./ tells the computer to look in the current folder for the program.

You should see "Hello, World!" printed in the terminal. If you don't, don't worry! Check these common issues:

  • Make sure you saved your hello.c file after pasting the code.
  • Check that you typed the commands exactly as shown, including the ./ before hello.
  • If you see any error messages, read them carefully. They often give clues about what went wrong.

Congratulations! You've just written and run your first C program.

Adding More Output

Now that we've got our first program running, let's expand it a bit. We'll modify our program to print multiple lines. This will show you how easy it is to add more output to your programs.

Open your hello.c file in the WebIDE. You can do this by clicking on the file name in the file explorer on the left side of the screen.

Replace the contents of hello.c with this new code:

#include <stdio.h>

int main() {
    printf("Hello, World!\n");
    printf("Welcome to C programming!\n");
    printf("This is your first C lab.\n");
    return 0;
}

This program uses printf three times to print three different lines. Let's break down what's new:

  • We've added two more printf lines, each ending with \n.
  • The \n at the end of each string moves to a new line. This is why each message will appear on a separate line when we run the program.
  • Notice how each line ends with a semicolon (;). In C, we use semicolons to show where each instruction ends.
alt text

Compile and run the program using the same commands as before:

gcc hello.c -o hello
./hello

You should now see three lines of text printed in the terminal. If you don't see all three lines, here are some things to check:

  • Make sure you saved the file after making changes.
  • Check that each line ends with \n inside the quotation marks.
  • Look for any missing semicolons at the end of the printf lines.

This demonstrates how you can use multiple printf statements to print several lines of text. You can add as many lines as you want using this method!

Using a Simple Variable

For our final step, let's introduce the concept of variables. A variable is like a labeled box where we can store information. In this case, we'll use a variable to store a name, which we'll then use in our greeting.

Create a new file in your WebIDE and name it greeting.c. Here's how:

  1. Right-click in the file explorer (left sidebar) and select "New File".
  2. Name the new file greeting.c.
  3. Click on greeting.c to open it in the editor.

Then, copy and paste the following code:

#include <stdio.h>

int main() {
    char name[] = "LabEx";
    printf("Hello, %s!\n", name);
    printf("Welcome to C programming, %s!\n", name);
    return 0;
}

This program introduces a few new concepts:

  • char name[] = "LabEx";: This creates a variable called name and stores the text "LabEx" in it.
    • char[] tells C that name will hold a series of characters (text).
    • You can change "LabEx" to any name you like!
  • We use the name variable in our printf statements to personalize our messages.
    • Notice how we use %s in the text and then , name at the end of printf. This tells C to insert the value of name where %s appears.
alt text

Compile and run the program:

gcc greeting.c -o greeting
./greeting

You should see personalized greeting messages in the terminal. If you changed "LabEx" to a different name, you'll see that name in the output.

If you're not seeing the expected output, here are some things to check:

  • Make sure you've saved the greeting.c file after pasting the code.
  • Check that you've typed the compilation command correctly: gcc greeting.c -o greeting (note that we're using greeting.c and greeting here, not hello.c and hello).
  • Ensure that the name variable is defined before it's used in the printf statements.

Try changing the name in the name[] = "LabEx"; line to your own name, then compile and run the program again. You'll see how variables allow us to easily change parts of our program!

Summary

Congratulations! You've completed your first C Lab and taken your first steps into the world of C programming. Let's recap what you've learned:

  1. How to write and run a basic C program

    • You learned about the structure of a C program, including the main() function.
    • You used gcc to compile your code and ./ to run the resulting program.
  2. How to use printf to display output

    • You used printf to print messages to the screen.
    • You learned about \n to move to a new line after printing.
  3. How to use multiple printf statements to print several lines

    • You saw how easy it is to add more output to your program.
  4. How to use a simple character array variable to store and display text

    • You learned how to create a char[] variable and give it a value.
    • You saw how to use a variable in your printf statements to personalize output.

These are the building blocks of C programming. You've made an excellent start!

Remember, learning to program is a journey, and you've just taken your first steps. Here are some tips as you continue learning:

  • Don't be afraid to experiment! Try changing the text in your printf statements or the value of your name variable.
  • If you encounter errors, read the error messages carefully. They often provide clues about what went wrong.
  • Practice is key. Try writing small programs that use what you've learned here.

Your next adventure in C is just around the corner. Keep coding, and have fun exploring the world of C!

Other C Tutorials you may like