Bird
Raised Fist0
Cprogramming~10 mins

Using scanf for input - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Using scanf for input
Start Program
↓
Declare variables
↓
Call scanf
↓
Wait for user input
↓
Store input in variables
↓
Use input in program
↓
End Program
The program declares variables, calls scanf to get user input, waits for input, stores it, then uses it.
Execution Sample
C
#include <stdio.h>
int main() {
  int age;
  scanf("%d", &age);
  printf("Age: %d\n", age);
  return 0;
}
Reads an integer from the user and prints it.
Execution Table
StepActionInput ProvidedVariable 'age' ValueOutput
1Program starts, variable 'age' declaredN/AundefinedN/A
2scanf called, waiting for inputN/AundefinedN/A
3User inputs '25' and presses Enter2525N/A
4printf prints the value of 'age'N/A25Age: 25
5Program endsN/A25N/A
💡 Program ends after printing the input value.
Variable Tracker
VariableStartAfter scanf inputFinal
ageundefined2525
Key Moments - 2 Insights
Why do we use &age in scanf instead of just age?
scanf needs the address of the variable to store the input there. Using &age gives scanf the memory location to write the value, as shown in step 3 of the execution_table.
What happens if the user inputs a non-integer value?
scanf expects an integer for %d. If the input is not an integer, scanf will fail to store a valid value, and 'age' may remain unchanged or undefined. This is why input must match the format specifier.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'age' immediately after scanf reads input?
A25
Bundefined
C0
DInput string
💡 Hint
Check the 'Variable age Value' column at Step 3 in the execution_table.
At which step does the program wait for user input?
AStep 1
BStep 2
CStep 4
DStep 5
💡 Hint
Look for the step where scanf is called and waiting for input in the execution_table.
If the user enters '30' instead of '25', how does the variable_tracker change?
AThe output changes but 'age' stays 25
BThe variable 'age' remains undefined
CThe 'After scanf input' and 'Final' values for 'age' become 30
DThe program crashes
💡 Hint
Variable_tracker shows the value stored after scanf input; changing input changes stored value.
Concept Snapshot
scanf("%d", &var) reads an integer from user input.
Use &var to pass variable address.
Input must match format specifier.
scanf waits for input during execution.
Value is stored in variable for later use.
Full Transcript
This program shows how scanf reads input from the user. First, it declares an integer variable 'age'. Then, scanf is called with "%d" and &age, which means it waits for the user to type an integer and press Enter. The input is stored in 'age'. Finally, printf prints the stored value. The execution table shows each step: starting the program, waiting for input, storing input, printing output, and ending. The variable tracker shows how 'age' changes from undefined to the input value. Remember, scanf needs the address of the variable to store the input. If input does not match the expected type, scanf may fail to store correctly.

Practice

(1/5)
1. What is the purpose of the scanf function in C?
easy
A. To read input from the user during program execution
B. To print output to the screen
C. To declare variables
D. To perform mathematical calculations

Solution

  1. Step 1: Understand the role of scanf

    scanf is used to get input from the user while the program runs.
  2. Step 2: Differentiate from other functions

    Functions like printf print output, but scanf reads input.
  3. Final Answer:

    To read input from the user during program execution -> Option A
  4. Quick Check:

    scanf reads input [OK]
Hint: scanf reads input, printf prints output [OK]
Common Mistakes:
  • Confusing scanf with printf
  • Thinking scanf declares variables
  • Assuming scanf performs calculations
2. Which of the following is the correct way to read an integer variable num using scanf?
easy
A. scanf("%s", &num);
B. scanf("%d", num);
C. scanf("%f", &num);
D. scanf("%d", &num);

Solution

  1. Step 1: Identify the correct format specifier for integer

    Use %d to read an integer.
  2. Step 2: Use the address operator for variables

    Use &num to pass the address of the variable to scanf.
  3. Final Answer:

    scanf("%d", &num); -> Option D
  4. Quick Check:

    Integer input needs %d and &variable [OK]
Hint: Use & before variable except arrays [OK]
Common Mistakes:
  • Omitting & before variable
  • Using wrong format specifier like %f or %s
  • Passing variable instead of its address
3. What will be the output of the following code if the user inputs 25?
int age;
scanf("%d", &age);
printf("Age: %d", age);
medium
A. Age: 0
B. Age: 25
C. Age: %d
D. Compilation error

Solution

  1. Step 1: Understand input reading

    scanf("%d", &age); reads the integer 25 and stores it in age.
  2. Step 2: Print the stored value

    printf("Age: %d", age); prints the string with the value of age, which is 25.
  3. Final Answer:

    Age: 25 -> Option B
  4. Quick Check:

    scanf stores input, printf prints it [OK]
Hint: scanf stores input, printf prints variable [OK]
Common Mistakes:
  • Forgetting & in scanf causing runtime error
  • Using wrong format specifier in printf
  • Expecting scanf to print output
4. Identify the error in the following code snippet:
int x;
printf("Enter a number: ");
scanf("%d", x);
printf("You entered %d", x);
medium
A. Missing & before variable x in scanf
B. Wrong format specifier in scanf
C. Missing semicolon after printf
D. Variable x not declared

Solution

  1. Step 1: Check scanf usage

    scanf("%d", x); is incorrect because it should use &x to pass the address.
  2. Step 2: Confirm other parts are correct

    Format specifier is correct, semicolons are present, and x is declared.
  3. Final Answer:

    Missing & before variable x in scanf -> Option A
  4. Quick Check:

    scanf needs &variable for input [OK]
Hint: Always use & before variables in scanf except arrays [OK]
Common Mistakes:
  • Omitting & causing segmentation fault
  • Using wrong format specifier
  • Assuming scanf prints output
5. You want to read a string (word) from the user into a character array name[20]. Which of the following scanf statements is correct and safe to avoid buffer overflow?
hard
A. scanf("%s", name);
B. scanf("%s", &name);
C. scanf("%19s", name);
D. scanf("%20s", &name);

Solution

  1. Step 1: Understand string input with scanf

    To read a string into name, use %s without & because arrays decay to pointers.
  2. Step 2: Prevent buffer overflow

    Limit input length to 19 characters using %19s to leave space for the null terminator.
  3. Final Answer:

    scanf("%19s", name); -> Option C
  4. Quick Check:

    Limit string input length with %19s [OK]
Hint: Use %19s for 20-char array to avoid overflow [OK]
Common Mistakes:
  • Using & with arrays in scanf
  • Not limiting input length causing overflow
  • Using wrong format specifier for strings
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