2.5 Lab - Getting User Input in C

Introduction

In this lab, you will learn how to get input from the user using the scanf function in C. You will write a program that prompts the user for their name and age, captures the input, and then displays a message using the provided information. This is an important step in understanding how programs interact with users in real-world applications.

Objectives

By the end of this lab, you will be able to:

  1. Understand how to use the scanf function to read user input in C.
  2. Write a program that accepts input from the user.
  3. Compile and run the program in a Linux environment using gcc.
  4. Display output based on the user's input.

Lab Steps


Step 1: Open the Terminal

  1. Open the terminal on your Linux machine.
  2. Navigate to your working directory, or use the directory you created in the previous lab:
    cd c_programs
    

Step 2: Create a New C Program

  1. Use the Nano editor to create a new C program file:

    nano user_input.c
    
  2. In the Nano editor, type the following code to prompt the user for their name and age:

    #include <stdio.h>
    
    int main() {
        char name[50];
        int age;
    
        // Ask the user for their name
        printf("Enter your name: ");
        // Get the user's name (string input)
        scanf("%s", name);
    
        // Ask the user for their age
        printf("Enter your age: ");
        // Get the user's age (integer input)
        scanf("%d", &age);
    
        // Display the user's input
        printf("Hello, %s! You are %d years old.\n", name, age);
    
        return 0;
    }
    
  3. Save the file by pressing CTRL + O, then press Enter. Exit the Nano editor by pressing CTRL + X.


Step 3: Compile the Program Using gcc

  1. To compile your C program, use the gcc compiler. Run the following command:

    gcc user_input.c -o user_input
    
  2. If there are no errors, the command will create an executable file named user_input. You can list the files to confirm:

    ls
    

    You should see user_input in the list.


Step 4: Run the Program

  1. Now that the program is compiled, run it by typing:

    ./user_input
    
  2. The program will prompt you for your name and age. For example:

    Enter your name: John
    Enter your age: 25
    
  3. After entering the name and age, the program will display a message like this:

    Hello, John! You are 25 years old.
    

Step 5: Experimenting with Different Inputs

  1. Try running the program multiple times with different names and ages to see how it responds. For example:
    Enter your name: Alice
    Enter your age: 30
    
    The program should display:
    Hello, Alice! You are 30 years old.
    

Step 6: Understanding the Code


Summary

In this lab, you learned how to use the scanf function to get input from the user in C. You built a program that asks the user for their name and age and then prints a message based on their input. This is a crucial concept, as user interaction forms the foundation of many C programs. By using scanf, you can handle different types of input and create interactive programs that respond to user-provided data.