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.
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.
- Open the terminal in the WebIDE. Verify you're in the default project directory:
pwd
Example output:
/home/labex/project

This command confirms you're in the correct starting directory. The /home/labex/project is the default workspace for your lab activities.
- 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.
- 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.
- Open the file in the WebIDE editor by clicking on the file name in the file explorer or using the
Open Fileoption. - 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 likeprintf()andscanf().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.
- Save the file using Ctrl+S or selecting
Savefrom theFilemenu.
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.
- Open the
user_input.cfile in the WebIDE editor. - 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 thenamearray.- The
%sformat specifier is used for reading strings. - Note that
scanf()with%sreads until it encounters a space, so it works best with single-word names.
- The
printf("Hello, %s!\n", name);prints a greeting using the entered name.- The
%sin the format string is replaced by the value ofname.
- The
- Save the file using Ctrl+S or selecting
Savefrom theFilemenu.
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.
- Open the
user_input.cfile in the WebIDE editor. - 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 theagevariable.- The
%dformat specifier is used for reading integers. - The
&beforeageprovides the memory address where the input should be stored.
- The
- The final
printf()now includes both name and age in the output message.
- Save the file using Ctrl+S or selecting
Savefrom theFilemenu.
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.
- Open the terminal in the WebIDE and navigate to the project directory:
cd ~/project
- 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.
- 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
- 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.



