Create User Input Program in C

CCBeginner
Practice Now

Introduction

In this lab, you will create a user input program in C. You will set up the development environment, write the basic program structure, implement user input for name and age, and then compile and run the program. The goal is to learn how to accept and process user input in a C program.

The lab guides you through the necessary steps, starting with setting up the development environment and creating a new C source file. You will then add the basic program structure, including the main function, and proceed to implement the functionality to accept user input for their name and age. Finally, you will compile and run the program to see the results.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL c(("C")) -.-> c/BasicsGroup(["Basics"]) c(("C")) -.-> c/CompoundTypesGroup(["Compound Types"]) c(("C")) -.-> c/UserInteractionGroup(["User Interaction"]) c/BasicsGroup -.-> c/variables("Variables") c/CompoundTypesGroup -.-> c/strings("Strings") c/UserInteractionGroup -.-> c/user_input("User Input") c/UserInteractionGroup -.-> c/output("Output") subgraph Lab Skills c/variables -.-> lab-438242{{"Create User Input Program in C"}} c/strings -.-> lab-438242{{"Create User Input Program in C"}} c/user_input -.-> lab-438242{{"Create User Input Program in C"}} c/output -.-> lab-438242{{"Create User Input Program in C"}} end

Set Up the Development Environment

In this step, we will set up our development environment for creating a C program to read user input. We'll use the WebIDE to create and manage our project files.

  1. Open the terminal in the WebIDE. Verify you're in the default project directory:
pwd

Example output:

/home/labex/project
Terminal showing project directory

This command confirms you're in the correct starting directory. The /home/labex/project is the default workspace for your lab activities.

  1. Verify the GCC compiler is installed:
gcc --version

Example output:

gcc (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0
Copyright (C) 2021 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

This command checks that the GNU Compiler Collection (GCC) is available, which we'll use to compile our C program.

Write the Basic Program Structure

In this step, we will create the basic structure of our C program to prepare for user input functionality.

  1. Create a new C source file in the project directory:
touch user_input.c

The touch command creates a new empty file named user_input.c in the current directory.

  1. Open the file in the WebIDE editor by clicking on the file name in the file explorer or using the Open File option.
  2. Add the basic C program structure to user_input.c:
#include <stdio.h>

int main() {
    // Program code will go here
    return 0;
}

Let's break down this basic structure:

  • #include <stdio.h> includes the standard input/output library, which provides functions like printf() and scanf().
  • int main() is the main function where program execution begins.
  • The empty main() function currently just returns 0, indicating successful execution.
  • The curly braces { } define the body of the main function.
  1. Save the file using Ctrl+S or selecting Save from the File menu.

Implement User Input for Name

In this step, we will modify our C program to read and display the user's name using input functions.

  1. Open the user_input.c file in the WebIDE editor.
  2. Update the program to include name input functionality:
#include <stdio.h>

int main() {
    char name[100];

    printf("Enter your name: ");
    scanf("%s", name);

    printf("Hello, %s!\n", name);

    return 0;
}

Let's break down the new code:

  • char name[100]; declares a character array (string) that can hold up to 99 characters plus the null terminator.
  • printf("Enter your name: "); prompts the user to enter their name.
  • scanf("%s", name); reads a string from user input and stores it in the name array.
    • The %s format specifier is used for reading strings.
    • Note that scanf() with %s reads until it encounters a space, so it works best with single-word names.
  • printf("Hello, %s!\n", name); prints a greeting using the entered name.
    • The %s in the format string is replaced by the value of name.
  1. Save the file using Ctrl+S or selecting Save from the File menu.

We will compile and run the program to test the functionality in the final step, so stay tuned!

Add Age Input

In this step, we will extend our program to read and display the user's age alongside their name.

  1. Open the user_input.c file in the WebIDE editor.
  2. Update the program to include age input functionality:
#include <stdio.h>

int main() {
    char name[100];
    int age;

    printf("Enter your name: ");
    scanf("%s", name);

    printf("Enter your age: ");
    scanf("%d", &age);

    printf("Hello, %s! You are %d years old.\n", name, age);

    return 0;
}

Let's break down the new code additions:

  • int age; declares an integer variable to store the user's age.
  • printf("Enter your age: "); prompts the user to enter their age.
  • scanf("%d", &age); reads an integer from user input and stores it in the age variable.
    • The %d format specifier is used for reading integers.
    • The & before age provides the memory address where the input should be stored.
  • The final printf() now includes both name and age in the output message.
  1. Save the file using Ctrl+S or selecting Save from the File menu.

Compile and Run the Program

In this final step, we will compile our C program and run it to interact with the user input functionality.

  1. Open the terminal in the WebIDE and navigate to the project directory:
cd ~/project
  1. Compile the program using the GCC compiler:
gcc user_input.c -o user_input

This command compiles the user_input.c source file and creates an executable named user_input. If there are any errors in your code, error messages will be displayed here.

  1. Run the compiled program:
./user_input

Example interaction:

Enter your name: Alice
Enter your age: 25
Hello, Alice! You are 25 years old.

When you run the program:

  • First, you'll be prompted to enter your name
  • Then, you'll be asked to enter your age
  • The program will display a personalized greeting with your name and age
  1. Try running the program multiple times with different names and ages to see how it works.

Summary

In this lab, you set up the development environment by creating a dedicated project directory, verifying the GCC compiler installation, and writing the basic structure of a C program. You then implemented user input functionality, allowing the program to read the user's name and age. Finally, you compiled and ran the program to see the results.

The key learning points covered in this lab include setting up a C programming project, understanding the basic program structure, and incorporating user input using the scanf() function. These foundational skills are essential for building more complex C applications that interact with users.