Bird
Raised Fist0
Cprogramming~10 mins

Multiple input and output in C - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Multiple input and output
Start Program
↓
Prompt for Inputs
↓
Read Inputs
↓
Process Inputs
↓
Print Outputs
↓
End Program
The program starts, asks the user for multiple inputs, reads them, processes the data, then prints multiple outputs before ending.
Execution Sample
C
#include <stdio.h>
int main() {
  int a, b, sum, diff;
  scanf("%d %d", &a, &b);
  sum = a + b;
  diff = a - b;
  printf("Sum=%d Diff=%d\n", sum, diff);
  return 0;
}
This program reads two integers, calculates their sum and difference, then prints both results.
Execution Table
StepActionInput ValuesVariables UpdatedOutput
1Program starts-a=undefined, b=undefined, sum=undefined, diff=undefined-
2Read inputs using scanfUser inputs: 7 3a=7, b=3-
3Calculate sum-sum=10-
4Calculate difference-diff=4-
5Print outputs--Sum=10 Diff=4
6Program ends---
💡 Program ends after printing sum and difference of inputs.
Variable Tracker
VariableStartAfter InputAfter Sum CalculationAfter Difference CalculationFinal
aundefined7777
bundefined3333
sumundefinedundefined101010
diffundefinedundefinedundefined44
Key Moments - 3 Insights
Why do we use &a and &b in scanf?
Because scanf needs the memory address to store the input values. See execution_table step 2 where inputs update variables a and b.
Why are sum and diff calculated after reading inputs?
Because sum and diff depend on a and b values. Until inputs are read (step 2), sum and diff cannot be calculated (steps 3 and 4).
Why does printf print both sum and diff in one line?
Because the format string includes both %d placeholders, so both values are printed together at step 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2, what are the values of variables a and b?
Aa=undefined, b=undefined
Ba=7, b=3
Ca=10, b=4
Da=3, b=7
💡 Hint
Check the 'Variables Updated' column at step 2 in execution_table.
At which step is the difference between a and b calculated?
AStep 4
BStep 3
CStep 2
DStep 5
💡 Hint
Look for 'Calculate difference' action in execution_table.
If the user inputs 5 and 2 instead of 7 and 3, what will be the output at step 5?
ASum=10 Diff=4
BSum=5 Diff=2
CSum=7 Diff=3
D3=ffiD 7=muS
💡 Hint
Sum and difference depend on inputs; check variable_tracker for calculation logic.
Concept Snapshot
Multiple input and output in C:
- Use scanf("%d %d", &a, &b) to read multiple inputs.
- Store inputs in variables.
- Perform calculations using inputs.
- Use printf("Sum=%d Diff=%d", sum, diff) to print multiple outputs.
- Inputs must be read before processing.
- Outputs can be printed together in one statement.
Full Transcript
This program starts by prompting the user to enter two integers. It reads these integers using scanf, storing them in variables a and b. Then it calculates the sum and difference of these two numbers and stores the results in sum and diff. Finally, it prints both results in one line using printf. The program ends after printing. The key steps are reading inputs, processing them, and printing outputs. Variables change values step by step as shown in the execution table and variable tracker.

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

  1. 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.
  2. Step 2: Understand printf with multiple outputs

    The printf function prints the values of a and b in the same order they were read.
  3. Final Answer:

    Reads two integers from input and prints them in the same order. -> Option C
  4. 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

  1. 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 &.
  2. Step 2: Identify correct option

    scanf("%d %d %d", &a, &b, &c); uses correct format string and & before variables, so it is correct.
  3. Final Answer:

    scanf("%d %d %d", &a, &b, &c); -> Option A
  4. 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

  1. Step 1: Read input values

    scanf reads 3 into x and 7 into y.
  2. Step 2: Calculate and print output

    printf prints x (3), y (7), and x + y (3 + 7 = 10).
  3. Final Answer:

    3 7 10 -> Option B
  4. 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

  1. Step 1: Check scanf arguments

    scanf requires addresses of variables, so &a and &b are needed.
  2. Step 2: Identify error

    Code passes variables a and b directly, causing undefined behavior or crash.
  3. Final Answer:

    Missing & before variables in scanf. -> Option D
  4. 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

  1. Step 1: Check input reading correctness

    scanf must use &a and &b to read two integers correctly.
  2. Step 2: Check output matches sum and product

    printf should print sum (a + b) first, then product (a * b) separated by space.
  3. Final Answer:

    int a, b; scanf("%d %d", &a, &b); printf("%d %d", a + b, a * b); -> Option A
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard