📖 Scenario: You are creating a simple program to record the names and ages of three friends. You will input their names and ages, then display this information clearly.
🎯 Goal: Build a C program that takes the names and ages of three friends as input and then prints each friend's name with their age.
📋 What You'll Learn
Use arrays to store names and ages
Use a loop to input data for three friends
Use a loop to output the stored data
Use scanf and printf for input and output
💡 Why This Matters
🌍 Real World
This kind of program helps in collecting and displaying simple records like names and ages, useful in small databases or contact lists.
💼 Career
Understanding multiple inputs and outputs is essential for many programming tasks such as data entry, processing user information, and building interactive applications.
Progress0 / 4 steps
1
Create arrays for names and ages
Declare a 2D character array called names with size 3 by 20 to store three names, and an integer array called ages with size 3 to store their ages.
C
Hint
Use char names[3][20]; to store three names each up to 19 characters plus null terminator.
2
Add a loop to input names and ages
Add a for loop with variable i from 0 to less than 3. Inside the loop, use scanf("%19s", names[i]) to input each name and scanf("%d", &ages[i]) to input each age.
C
Hint
Use a for loop to input data for each friend.
3
Add a loop to output names and ages
Add a for loop with variable i from 0 to less than 3. Inside the loop, use printf("%s is %d years old\n", names[i], ages[i]) to print each friend's name and age.
C
Hint
Use a for loop to print each friend's information.
4
Print the final output
Run the program and input these three friends' data exactly in this order: Alice 25 Bob 30 Charlie 22 Then print the output exactly as: Alice is 25 years old Bob is 30 years old Charlie is 22 years old
C
Hint
Type the names and ages exactly as shown, then check the printed output.
Practice
(1/5)
1. What does the following C code do? int a, b; scanf("%d %d", &a, &b); printf("%d %d", a, b);
easy
A. Prints two integers without reading input.
B. Reads one integer and prints it twice.
C. Reads two integers from input and prints them in the same order.
D. Reads two integers but prints only the first one.
Solution
Step 1: Understand scanf with multiple inputs
The scanf function reads two integers from the user input and stores them in variables a and b.
Step 2: Understand printf with multiple outputs
The printf function prints the values of a and b in the same order they were read.
Final Answer:
Reads two integers from input and prints them in the same order. -> Option C
Quick Check:
scanf and printf with two %d = same two integers [OK]
Hint: Match each %d with a variable and & in scanf [OK]
Common Mistakes:
Forgetting & before variables in scanf
Mixing order of variables in printf
Using wrong format specifiers
2. Which of the following is the correct syntax to read three integers a, b, c in C?
easy
A. scanf("%d %d %d", &a, &b, &c);
B. scanf("%d %d %d", a, b, c);
C. scanf("%d, %d, %d", &a, &b, &c);
D. scanf("%d %d %d", *a, *b, *c);
Solution
Step 1: Check scanf format string and arguments
The format string should have three %d separated by spaces, and variables must be passed by address using &.
Step 2: Identify correct option
scanf("%d %d %d", &a, &b, &c); uses correct format string and & before variables, so it is correct.
Final Answer:
scanf("%d %d %d", &a, &b, &c); -> Option A
Quick Check:
Use & with variables in scanf [OK]
Hint: Always use & before variables in scanf [OK]
Common Mistakes:
Omitting & before variables
Using commas inside format string incorrectly
Using pointers incorrectly with *
3. What is the output of this C program?
int x, y; scanf("%d %d", &x, &y); printf("%d %d %d", x, y, x + y);
Input: 3 7
medium
A. 10 10 20
B. 3 7 10
C. 7 3 10
D. 3 7 37
Solution
Step 1: Read input values
scanf reads 3 into x and 7 into y.
Step 2: Calculate and print output
printf prints x (3), y (7), and x + y (3 + 7 = 10).
Final Answer:
3 7 10 -> Option B
Quick Check:
3 + 7 = 10 [OK]
Hint: Add variables inside printf for combined output [OK]
Common Mistakes:
Mixing order of variables in printf
Confusing concatenation with addition
Not using & in scanf
4. Find the error in this code snippet:
int a, b; scanf("%d %d", a, b); printf("%d %d", a, b);
medium
A. Missing semicolon after scanf.
B. Wrong format specifier in scanf.
C. printf format specifiers do not match variables.
D. Missing & before variables in scanf.
Solution
Step 1: Check scanf arguments
scanf requires addresses of variables, so &a and &b are needed.
Step 2: Identify error
Code passes variables a and b directly, causing undefined behavior or crash.
Final Answer:
Missing & before variables in scanf. -> Option D
Quick Check:
scanf needs & before variables [OK]
Hint: Always use & before variables in scanf [OK]
Common Mistakes:
Forgetting & in scanf
Assuming variables auto-pass by address
Confusing scanf and printf syntax
5. Write a C program snippet that reads two integers and prints their sum and product separated by a space. Which code correctly does this?
hard
A. int a, b; scanf("%d %d", &a, &b); printf("%d %d", a + b, a * b);
B. int a, b; scanf("%d %d", a, b); printf("%d %d", a + b, a * b);
C. int a, b; scanf("%d %d", &a, &b); printf("%d %d", a - b, a / b);
D. int a, b; scanf("%d %d", &a, &b); printf("%d %d", a * b, a + b);
Solution
Step 1: Check input reading correctness
scanf must use &a and &b to read two integers correctly.
Step 2: Check output matches sum and product
printf should print sum (a + b) first, then product (a * b) separated by space.
Final Answer:
int a, b;
scanf("%d %d", &a, &b);
printf("%d %d", a + b, a * b); -> Option A
Quick Check:
scanf with & and printf sum then product [OK]
Hint: Print sum first, product second with correct scanf [OK]
Common Mistakes:
Forgetting & in scanf
Swapping sum and product order
Using wrong operators in printf
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.