0
0
Cprogramming~10 mins

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

Choose your learning style9 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.