Print Name in C: Read User Input with scanf (30-Sec Answer)
How to read a name and age from the user, then print them back — with code, output, and plain-English walkthrough.
About the Author
Kailas Warade
C, SQL and Python Instructor
Kailas teaches C programming, SQL and Python to beginners and interview candidates. He writes step-by-step tutorials for Sankalan Data Tech and focuses on plain explanations that actually help students pass exams and get jobs.
Quick Answer: Print a Name in C
Here is the answer: use scanf("%s", name) to read a name and printf("%s", name) to print it. Below is the full 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.
Remember: %s for strings, %d for integers. Use & before integers, not before arrays.
What You Will Learn on This Page
- How User Input Works in C
- The Full Program
- Line-by-Line Walkthrough
- Variations (Full Names, Multiple Inputs, More Fields)
- scanf vs fgets: Which One to Use
- Common Mistakes
- Where This Shows Up in Real Software
- Practice Questions
Here is the answer: use scanf("%s", name) to read a name and printf("%s", name) to print it. That is the whole thing in one line. The rest of this page shows the full program, explains each line, and covers the two variations you will need most often — full names with spaces, and reading multiple fields in one go.
You can extend the same pattern to read an age, a city, a CGPA, or any other input. Once you understand how scanf and printf work, the rest is just picking the right format specifier.
How User Input Works in C
Every C program that talks to a user relies on two functions from stdio.h. One reads, one writes.
The Two Functions You Will Use Everywhere
- printf() — writes output to the screen
- scanf() — reads input from the keyboard
- gets() — reads a line but is unsafe and deprecated. Do not use it.
- fgets() — the safe alternative for reading strings with spaces
Format Specifiers You Need to Know
| 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); |
The one rule to remember: with strings (%s), do not use the & before the array name. The array name is already the address. For integers, floats, and characters, you must use &, because you are telling scanf where to store the value.
The Full Program
Read Name and Age, Then Print Them
#include <stdio.h>
int main() {
// Declare the variables
char name[50];
int age;
// Read the name
printf("Enter your name: ");
scanf("%s", name);
// Read the age
printf("Enter your age: ");
scanf("%d", &age);
// Print everything back
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.
Line-by-Line Walkthrough
-
Include the header.
#include <stdio.h>brings inprintfandscanf. Without it, the compiler will complain that these functions are not declared. -
Declare the variables.
char name[50];creates a string that can hold up to 49 characters plus the null terminator at the end.int age;creates a single integer slot. These two variables are where the input will be stored. -
Prompt the user for the name.
printf("Enter your name: ");prints a message so the user knows what to type. Without it, the program would just sit there, and the user would have no idea why. -
Read the name.
scanf("%s", name);waits for the user to type something and press Enter. The%stells it to read a string. Note: no&here. Arrays are already addresses, so adding&would be wrong. -
Prompt for the age.
printf("Enter your age: ");another prompt, same idea. -
Read the age.
scanf("%d", &age);reads an integer. You need the&here.ageis a single integer, andscanfneeds to know its address so it can write the value into it. -
Print everything back.
The three
printfcalls at the end display the name, the age, and a friendly greeting. Each%sis replaced by the string, each%dby the integer.
Variations You Will Actually Use
Variation 1: Reading a Full Name with Spaces (fgets)
scanf("%s", name) stops at the first space. So if the user types "John Doe", name ends up holding only "John". For full names, use fgets instead.
#include <stdio.h>
int main() {
char name[100];
int age;
printf("Enter your full name: ");
fgets(name, sizeof(name), stdin);
printf("Enter your age: ");
scanf("%d", &age);
printf("\nHello %sYou are %d years old.\n", name, age);
// Note: fgets keeps the newline at the end, so there is a line break after the name
return 0;
}
Output
Enter your full name: John Doe
Enter your age: 25
Hello John Doe
You are 25 years old.
Variation 2: Reading Two Values in One Line
You can read multiple values with a single scanf call if you separate the format specifiers by spaces. This is handy when you want to type the name and age together.
#include <stdio.h>
int main() {
char name[50];
int age;
printf("Enter name and age (separated by a space): ");
scanf("%s %d", name, &age);
printf("Name: %s, Age: %d\n", name, age);
return 0;
}
Output
Enter name and age (separated by a space): John 25
Name: John, Age: 25
Variation 3: Reading Name, Age, and City
Once you understand the pattern, adding more fields is just a matter of adding more variables and more prompts. This is the version you would write if you were building a simple registration form.
#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: Pune
--- User Information ---
Name: Alice
Age: 30
City: Pune
scanf vs fgets: Which One to Use
This is the single most common confusion beginners have, so let's clear it up properly. The two functions look like they do the same thing, but they behave very differently when the input contains a space.
| Feature | scanf("%s", name) | fgets(name, size, stdin) |
|---|---|---|
| Reads spaces? | No — stops at the first space | Yes — reads the entire line |
| Input "John Doe" | Stores only "John" | Stores "John Doe" |
| Buffer safety | Can overflow if the name is too long | Safe — respects the size limit |
| Trailing newline | No | Yes — you may need to strip it |
| When to use | Single-word input, numbers | Names, addresses, any text with spaces |
Simple rule: if the input can contain a space, use fgets. If it is a single word or a number, scanf is fine. Most real programs use both: fgets for names, scanf for ages and numeric fields.
Common Mistakes Beginners Make
- Forgetting the
&before an integer.scanf("%d", age);is wrong; it must bescanf("%d", &age);. This is the most common error in the first week of learning C. - Adding
&before a string.scanf("%s", &name);is wrong; it must bescanf("%s", name);. Arrays are already addresses. - Using
scanffor names with spaces. "John Doe" becomes "John". Usefgetsinstead. - Making the array too small.
char name[5];cannot hold "Rajesh". Use at leastchar name[50];unless you have a good reason not to. - Using
gets(). It was removed from the C standard in 2011 because it has no bounds checking. Never use it. Usefgets. - Ignoring what
scanfreturns.scanfreturns the number of items it successfully read. If a user types a letter when you expect an integer,scanfreturns 0 and the program misbehaves. Serious code checks the return value.
Where This Shows Up in Real Software
Reading a name and a number is not just a classroom exercise. The same pattern — prompt, read, validate, print — runs in almost every interactive program you have ever used.
- Registration forms. Any app that asks for your name and age uses exactly this pattern, just behind a graphical form instead of a terminal prompt.
- Student management systems. Recording a student's name, roll number, marks, and grade — each field is a
scanfcall waiting to happen. - Hospital check-in. Reading a patient's name, age, and ID number uses the same logic, at a much larger scale.
- Library systems. Issuing a book requires reading a member's name and ID, then reading the book's number. Same pattern.
In every one of these, the underlying C logic is what you have just seen: declare a variable, prompt for input, read it with the right format specifier, and store it. The rest is presentation.
Practice Questions
Try these on your own. Each one builds on the pattern you just learned.
- Write a program that reads a 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 full name with spaces using
fgetsand displays it. - Write a program that reads a student's name and marks in three subjects, then calculates and prints the average.
- Write a program that reads a user's address (street, city, zip code) and prints it formatted across three lines.
Hint: use fgets for strings with spaces, scanf with %d for integers. Remember to add & for non-array variables.
Why This Program Matters
- It is the first interactive program most students write. Everything before it reads predefined values. This is where the program starts talking to a real user.
- The pattern shows up everywhere. Once you know
scanfandprintf, every form, every prompt, every CLI tool follows the same shape. - It is asked in exams. Input/output questions are a standard part of the C programming syllabus and appear in almost every practical exam.
- It leads naturally to file I/O. Once you are comfortable reading from the keyboard, reading from a file is a small next step.
Frequently Asked Questions
How do I read a string (name) from a user in C?
Use scanf("%s", name); where name is a character array. For names with spaces, use fgets(name, sizeof(name), stdin); instead — %s stops at the first space.
How do I print a name in C?
Use printf("%s", name); where name holds the string. That is the whole answer. The format specifier %s tells printf to substitute the string at that spot.
How do I read an integer (age) from a user in C?
Use scanf("%d", &age); where age is an integer variable. The & operator is required, because scanf needs the address of the variable to store the value in it.
What is the difference between scanf and fgets in C?
scanf("%s", name) stops at the first space, so "John Doe" becomes just "John". fgets(name, sizeof(name), stdin) reads the entire line, spaces included. Use fgets for names, scanf for numbers and single words.
What is the correct format specifier for a string in C?
The format specifier for a string is %s. For a single character, use %c. For integers, %d. For floats, %f.
How do I print a string and an integer together in C?
Use printf with multiple format specifiers in one call: printf("Name: %s, Age: %d", name, age); Each specifier matches one argument in the order you pass them.
Related Tutorials
Previous Topic: Basic Input/Output in C | Next Topic: Address Operator in C