C Program to Read and Print User Name and Age
Learn how to take user input in C using scanf() and display it using printf()
About the Author
Sankalan Data Technologies Team
Programming and Data Technology Tutorials
This tutorial was created by the Sankalan Data Technologies team. Our instructors have experience teaching C programming, SQL, Python and other programming technologies to students and professionals. Content is written and reviewed by experienced trainers and developers.
Quick Answer: Read and Print User Name and Age
To read user input in C, use scanf() for reading and
printf() for displaying. Here's a simple program:
#include <stdio.h>
int main() {
char name[50];
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;
}
Output:
Enter your name: John
Enter your age: 25
Hello John! You are 25 years old.
Note: %s for strings, %d for integers, and & for storing values.
What You'll Learn in This Tutorial
- Understanding User Input in C
- Complete Program
- Step-by-Step Explanation
- Program Variations
- Common Mistakes
- Practice Questions
Taking user input is one of the most fundamental operations in any programming language. In C, we use scanf() to read input from the user and printf() to display output. This tutorial will teach you how to read a user's name (string) and age (integer) and display them back.
Whether you're building a simple program or a complex application, understanding how to interact with users through input and output is essential for any C programmer.
Understanding User Input in C
In C, user input is handled through the Standard Input/Output Library
(stdio.h). The two main functions for input and output are:
Key Functions for User Input/Output
- printf() - Displays output to the console
- scanf() - Reads input from the user
- gets() - Reads a line (deprecated, unsafe)
- fgets() - Safe alternative for reading strings
Input/Output Format Specifiers
| Data Type | Format Specifier | Example |
|---|---|---|
| String | %s |
scanf("%s", name); |
| Integer | %d |
scanf("%d", &age); |
| Character | %c |
scanf("%c", &ch); |
| Float | %f |
scanf("%f", &marks); |
Remember: When using scanf() with strings (%s),
you don't need the & operator because the array name already acts as a pointer.
For integers, floats, and characters, you must use &.
Complete Program: Read and Print User Name and Age
Program to Read Name and Age
#include <stdio.h>
int main() {
// Declare variables
char name[50];
int age;
// Get user input
printf("Enter your name: ");
scanf("%s", name);
printf("Enter your age: ");
scanf("%d", &age);
// Display output
printf("\n--- User Details ---\n");
printf("Name: %s\n", name);
printf("Age: %d\n", age);
printf("Hello %s! You are %d years old.\n", name, age);
return 0;
}
Output
Enter your name: John
Enter your age: 25
--- User Details ---
Name: John
Age: 25
Hello John! You are 25 years old.
Step-by-Step Explanation
-
Include stdio.h:
#include <stdio.h>- This header file contains the declarations forprintf()andscanf(). -
Declare Variables:
char name[50]; int age;-nameis a character array that can store up to 49 characters plus the null terminator.agestores an integer. -
Prompt for Name:
printf("Enter your name: ");- Displays a message asking for input. -
Read Name:
scanf("%s", name);- Reads a string from the user. Note:%sreads until whitespace, and no&is needed for arrays. -
Prompt for Age:
printf("Enter your age: ");- Displays a message asking for age. -
Read Age:
scanf("%d", &age);- Reads an integer. The&operator gets the address ofageto store the value. -
Display Output:
printf("Hello %s! You are %d years old.\n", name, age);- Prints both values using format specifiers.
Program Variations
Variation 1: Reading Name with Spaces (Using fgets)
#include <stdio.h>
int main() {
char name[100];
int age;
printf("Enter your full name: ");
fgets(name, sizeof(name), stdin); // Reads including spaces
printf("Enter your age: ");
scanf("%d", &age);
printf("\nHello %sYou are %d years old.\n", name, age);
// Note: fgets includes newline, so output may have extra line break
return 0;
}
Output
Enter your full name: John Doe
Enter your age: 25
Hello John Doe
You are 25 years old.
Variation 2: Reading Multiple Inputs in One Line
#include <stdio.h>
int main() {
char name[50];
int age;
printf("Enter name and age (separated by space): ");
scanf("%s %d", name, &age);
printf("Name: %s, Age: %d\n", name, age);
return 0;
}
Output
Enter name and age (separated by space): John 25
Name: John, Age: 25
Variation 3: Reading Name, Age, and City
#include <stdio.h>
int main() {
char name[50], city[50];
int age;
printf("Enter your name: ");
scanf("%s", name);
printf("Enter your age: ");
scanf("%d", &age);
printf("Enter your city: ");
scanf("%s", city);
printf("\n--- User Information ---\n");
printf("Name: %s\n", name);
printf("Age: %d\n", age);
printf("City: %s\n", city);
return 0;
}
Output
Enter your name: Alice
Enter your age: 30
Enter your city: New York
--- User Information ---
Name: Alice
Age: 30
City: New York
Common Mistakes to Avoid
- Forgetting & before variable -
scanf("%d", age);is wrong; should bescanf("%d", &age); - Using & with strings -
scanf("%s", &name);is wrong; should bescanf("%s", name); - Buffer overflow - Reading more characters than the array can hold. Always define large enough arrays.
- Mismatched format specifiers - Using
%dfor a string or%sfor an integer. - Ignoring return values -
scanf()returns the number of items successfully read. Always check it. - Using gets() - Deprecated and unsafe. Use
fgets()instead.
Practice Questions
Try these exercises to reinforce your understanding:
- Write a program that reads a user's first name, last name, and age, then prints a full sentence.
- Write a program that reads two integers and prints their sum, difference, product, and quotient.
- Write a program that reads a character and prints its ASCII value.
- Write a program that reads a string with spaces using fgets() and displays it.
- Write a program that reads a student's name, marks in 3 subjects, and calculates the average.
- Write a program that reads a user's address (street, city, zip code) and displays it formatted.
Hint: Use fgets() for strings with spaces, and scanf() with %d for integers. Remember to use & for non-array variables.
Why This Program Matters
- Foundation Concept: User input and output are fundamental to interactive programming.
- Real-World Use: Every interactive program needs to read user input and display results.
- Exam Relevance: Basic input/output questions are common in C programming exams.
- Building Block: Understanding this prepares you for more advanced topics like file I/O and data processing.
Frequently Asked Questions
How do I read a string (name) from user in C?
You can read a string in C using scanf() with the %s format specifier: scanf("%s", name); where name is a character array. Use fgets() for safer input that handles spaces.
How do I read an integer (age) from user in C?
To read an integer in C, use scanf() with the %d format specifier: scanf("%d", &age); where age is an integer variable. The & (address operator) is required to store the value in the variable.
What is the difference between scanf and gets in C?
scanf() with %s reads input until whitespace, so it cannot read strings with spaces. gets() reads an entire line including spaces, but it's unsafe and deprecated. fgets() is the safe alternative for reading strings with spaces.
How do I read a string with spaces in C?
Use fgets() to read strings with spaces: fgets(name, sizeof(name), stdin); This reads the entire line including spaces. Alternatively, use scanf("%[^\n]s", name); to read until newline.
What is the correct format specifier for string in C?
The format specifier for string (character array) in C is %s. For example: scanf("%s", str); or printf("%s", str); Use %c for individual characters.
How do I print a string and integer together in C?
Use printf() with multiple format specifiers: printf("Name: %s, Age: %d", name, age); This prints both the string and integer values in a single line.
Related Tutorials
Previous Topic: Basic Input/Output in C | Next Topic: Address Operator in C