Using scanf for input - Time & Space Complexity
When we use scanf to get input in C, it takes some time to read the data. We want to understand how this reading time changes as we ask for more inputs.
How does the time to read inputs grow when we increase the number of inputs?
Analyze the time complexity of the following code snippet.
#include <stdio.h>
int main() {
int n, x;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &x);
}
return 0;
}
This code reads an integer n and then reads n more integers from the user.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Reading an integer using
scanfinside the loop. - How many times: Exactly
ntimes, wherenis the number of inputs to read.
Each time we increase n, we do one more input read. So the total reading time grows directly with n.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 reads |
| 100 | 100 reads |
| 1000 | 1000 reads |
Pattern observation: The time grows in a straight line as we add more inputs.
Time Complexity: O(n)
This means the time to read inputs grows directly in proportion to how many inputs we read.
[X] Wrong: "Reading inputs with scanf always takes the same time no matter how many inputs."
[OK] Correct: Each input requires a separate read operation, so more inputs mean more time spent reading.
Understanding how input reading scales helps you reason about program speed and efficiency, a useful skill in many coding challenges and real projects.
"What if we read inputs using a single line with multiple values instead of one by one? How would the time complexity change?"
Practice
scanf function in C?Solution
Step 1: Understand the role of
scanfscanfis used to get input from the user while the program runs.Step 2: Differentiate from other functions
Functions likeprintfprint output, butscanfreads input.Final Answer:
To read input from the user during program execution -> Option AQuick Check:
scanf reads input [OK]
- Confusing scanf with printf
- Thinking scanf declares variables
- Assuming scanf performs calculations
num using scanf?Solution
Step 1: Identify the correct format specifier for integer
Use%dto read an integer.Step 2: Use the address operator for variables
Use&numto pass the address of the variable toscanf.Final Answer:
scanf("%d", &num); -> Option DQuick Check:
Integer input needs %d and &variable [OK]
- Omitting & before variable
- Using wrong format specifier like %f or %s
- Passing variable instead of its address
25?
int age;
scanf("%d", &age);
printf("Age: %d", age);Solution
Step 1: Understand input reading
scanf("%d", &age);reads the integer 25 and stores it inage.Step 2: Print the stored value
printf("Age: %d", age);prints the string with the value ofage, which is 25.Final Answer:
Age: 25 -> Option BQuick Check:
scanf stores input, printf prints it [OK]
- Forgetting & in scanf causing runtime error
- Using wrong format specifier in printf
- Expecting scanf to print output
int x;
printf("Enter a number: ");
scanf("%d", x);
printf("You entered %d", x);Solution
Step 1: Check scanf usage
scanf("%d", x);is incorrect because it should use&xto pass the address.Step 2: Confirm other parts are correct
Format specifier is correct, semicolons are present, andxis declared.Final Answer:
Missing & before variable x in scanf -> Option AQuick Check:
scanf needs &variable for input [OK]
- Omitting & causing segmentation fault
- Using wrong format specifier
- Assuming scanf prints output
name[20]. Which of the following scanf statements is correct and safe to avoid buffer overflow?Solution
Step 1: Understand string input with scanf
To read a string intoname, use%swithout & because arrays decay to pointers.Step 2: Prevent buffer overflow
Limit input length to 19 characters using%19sto leave space for the null terminator.Final Answer:
scanf("%19s", name); -> Option CQuick Check:
Limit string input length with %19s [OK]
- Using & with arrays in scanf
- Not limiting input length causing overflow
- Using wrong format specifier for strings
Ready to prove it?
Pass the test and this pattern moves unknown
