Introduction
In this lab, you will learn how to read user input in the C programming language using the scanf() function. The scanf() function is a powerful tool for reading input from users and is defined in the standard input/output library stdio.h. C is a strongly typed language that supports various data types. Throughout this lab, we will focus on using the char and int data types to read and display user input.
Set Up the Development Environment
In this step, we will set up our development environment and create a new C file for our program.
Open a terminal in the WebIDE. You should be in the
/home/labex/projectdirectory by default. If you're not sure, you can typepwd(print working directory) to check your current location.Create a new file named
user_input.cusing the following command:touch user_input.cThe
touchcommand creates a new, empty file if it doesn't exist, or updates the timestamp of an existing file.Open the
user_input.cfile in the WebIDE editor. You can do this by clicking on the file name in the file explorer on the left side of the WebIDE, or by using theOpen Fileoption in theFilemenu.
Write the Basic Program Structure
In this step, we will write the basic structure of our C program.
In the
user_input.cfile, add the following code:#include <stdio.h> int main() { // We will add our code here return 0; }Let's break this down:
#include <stdio.h>tells the compiler to include the standard input/output library. This library contains functions likeprintf()andscanf()that we'll use for input and output.int main()is the main function where our program starts executing. Every C program must have a main function.- The curly braces
{ }define the body of the main function. return 0;at the end of main indicates that the program has executed successfully.
Save the file. You can do this by pressing Ctrl+S or by selecting
Savefrom theFilemenu.
Implement User Input for Name
Now, let's implement the functionality to read the user's name.
Modify the
user_input.cfile to include the following code inside themain()function:#include <stdio.h> int main() { char name[100]; printf("Enter your name: "); scanf("%s", name); printf("Hello, %s!\n", name); return 0; }Here's what each new line does:
char name[100];declares an array of characters (a 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 the user input and stores it in thenamearray. The%sformat specifier is used for reading strings.printf("Hello, %s!\n", name);prints a greeting using the name entered by the user. The%sin the format string is replaced by the value ofname.
Save the file.
Add Age Input
Let's extend our program to also ask for the user's age.
Modify the
user_input.cfile to include age input:#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; }What's new here:
int age;declares an integer variable to store the user's age.- We've added another
printf()andscanf()pair to prompt for and read the age. scanf("%d", &age);reads an integer from the user input. The%dformat specifier is used for integers. Note the&beforeage- this is becausescanf()needs the memory address of the variable to store the input.- The final
printf()now includes the age in the output message.
Save the file.
Compile and Run the Program
In this final step, we will compile our C program and run it to see the results.
In the terminal, navigate to the directory containing your
user_input.cfile:cd /home/labex/projectThis step ensures you're in the right directory. If you're already there, you'll see a message saying "cd: no such file or directory", which you can ignore.
Compile the program using the GCC compiler:
gcc user_input.c -o user_inputThis command tells GCC to compile
user_input.cand create an executable nameduser_input. If there are any errors in your code, you'll see error messages here. If that happens, go back to your code, fix the errors, and try compiling again.Run the compiled program:
./user_inputThe
./tells the shell to look for the program in the current directory.

The program will prompt you to enter your name and age. Type your responses and press Enter after each input.
You should see a personalized greeting message with your name and age.
If you encounter any issues, double-check your code for typos and make sure you've saved all changes before compiling.
Summary
In this lab, you have learned how to read user input in C using the scanf() function. You created a program that prompts users for their name and age, reads the input, and displays a personalized greeting message. This exercise has introduced you to basic input/output operations in C, working with different data types (char arrays for strings and int for integers), and the process of compiling and running a C program.
Key takeaways:
- The
scanf()function is used to read input from the user. - Different format specifiers (
%sfor strings,%dfor integers) are used depending on the type of input you're reading. - When reading integers or other non-string types with
scanf(), you need to use the&operator to pass the address of the variable. - The compilation process turns your C code into an executable program.
These fundamental skills form the basis for more complex C programming tasks and will be valuable as you continue to learn and develop your programming abilities. Remember, practice is key in programming - try modifying this program or creating new ones to reinforce what you've learned!



