We use multiple input and output to handle more than one piece of information at the same time. This helps programs interact with users or other programs better.
Multiple input and output in C
scanf("%d %d", &var1, &var2); printf("%d %d", var1, var2);
Use scanf to read multiple inputs by listing format specifiers and variables separated by spaces.
Use printf to print multiple outputs by listing format specifiers and variables in order.
a and b.int a, b; scanf("%d %d", &a, &b);
a and b separated by a space.printf("%d %d", a, b);float x, y; scanf("%f %f", &x, &y); printf("%.2f %.2f", x, y);
This program asks the user to enter two numbers. It reads both numbers at once using scanf. Then it calculates their sum and product and prints both results separately.
#include <stdio.h> int main() { int num1, num2; printf("Enter two numbers separated by space: "); scanf("%d %d", &num1, &num2); int sum = num1 + num2; int product = num1 * num2; printf("Sum: %d\n", sum); printf("Product: %d\n", product); return 0; }
Always provide the address of variables to scanf using &.
Separate format specifiers in scanf and printf with spaces to match input/output format.
Be careful to match the data types in format specifiers with the variables you use.
Multiple input and output lets you handle several values at once.
Use scanf with multiple format specifiers and variable addresses to read inputs.
Use printf with multiple format specifiers and variables to print outputs.
Practice
int a, b;
scanf("%d %d", &a, &b);
printf("%d %d", a, b);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 CQuick Check:
scanf and printf with two %d = same two integers [OK]
- Forgetting & before variables in scanf
- Mixing order of variables in printf
- Using wrong format specifiers
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 AQuick Check:
Use & with variables in scanf [OK]
- Omitting & before variables
- Using commas inside format string incorrectly
- Using pointers incorrectly with *
int x, y;
scanf("%d %d", &x, &y);
printf("%d %d %d", x, y, x + y);
Input:
3 7Solution
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 BQuick Check:
3 + 7 = 10 [OK]
- Mixing order of variables in printf
- Confusing concatenation with addition
- Not using & in scanf
int a, b;
scanf("%d %d", a, b);
printf("%d %d", a, b);
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 DQuick Check:
scanf needs & before variables [OK]
- Forgetting & in scanf
- Assuming variables auto-pass by address
- Confusing scanf and printf syntax
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 AQuick Check:
scanf with & and printf sum then product [OK]
- Forgetting & in scanf
- Swapping sum and product order
- Using wrong operators in printf
Ready to prove it?
Pass the test and this pattern moves unknown
