Basic data types - Time & Space Complexity
We want to see how the time to run code changes when using basic data types.
How does the program's work grow as the input size changes?
Analyze the time complexity of the following code snippet.
#include <stdio.h>
int main() {
int a = 5;
float b = 3.14f;
char c = 'x';
double d = 2.71828;
return 0;
}
This code declares variables of basic data types and assigns values.
Look for any repeated actions or loops.
- Primary operation: Variable declarations and assignments.
- How many times: Each happens once, no loops or repetition.
Since there are no loops or repeated steps, the work stays the same no matter the input size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 4 (fixed declarations) |
| 100 | 4 (still fixed declarations) |
| 1000 | 4 (no change) |
Pattern observation: The number of operations does not grow with input size.
Time Complexity: O(1)
This means the program takes the same amount of time no matter how big the input is.
[X] Wrong: "Declaring more variables makes the program slower as input grows."
[OK] Correct: Variable declarations happen once and do not depend on input size, so they do not slow down the program as input grows.
Understanding that simple variable declarations run in constant time helps you explain how programs start and manage data efficiently.
"What if we added a loop that initializes an array of size n? How would the time complexity change?"
Practice
Solution
Step 1: Understand data types for numbers
Whole numbers without decimals are stored using theinttype in C.Step 2: Identify correct type for whole numbers
floatstores decimals,charstores characters, andboolstores true/false values.Final Answer:
int -> Option AQuick Check:
Whole numbers = int [OK]
- Confusing float with int for whole numbers
- Using char for numbers
- Thinking bool stores numbers
Solution
Step 1: Check variable declaration syntax
In C, to declare a float variable, use the keywordfloatfollowed by the variable name and value.Step 2: Match type with value
Assigning 3.14 (a decimal) to a float variable is correct. Other types like int, char, or bool cannot hold decimal values properly.Final Answer:
float number = 3.14; -> Option CQuick Check:
Decimal numbers use float type [OK]
- Using int for decimal values
- Assigning decimal to char
- Using bool for numbers
char letter = 'A';
int number = 5;
float decimal = 3.5;
printf("%c %d %.1f", letter, number, decimal);Solution
Step 1: Understand format specifiers
%cprints a character,%dprints an integer, and%.1fprints a float with one decimal place.Step 2: Match variables to specifiers
letter = 'A'prints 'A',number = 5prints 5, anddecimal = 3.5prints 3.5 with one decimal.Final Answer:
A 5 3.5 -> Option DQuick Check:
Format specifiers match output [OK]
- Printing char as int (shows ASCII code)
- Ignoring decimal precision
- Confusing format specifiers
bool flag = 1; char letter = "B"; int count = 10;
Solution
Step 1: Check bool assignment
Assigning 1 to bool is allowed since 1 means true in C.Step 2: Check char assignment
Characters must be in single quotes ('B'), not double quotes ("B") which are for strings.Final Answer:
char should use single quotes for characters -> Option AQuick Check:
char uses single quotes [OK]
- Using double quotes for char
- Thinking bool can't be 1 or 0
- Confusing int assignments
Solution
Step 1: Choose type for grade letter
Grade letters like 'A' are single characters, socharis the right type.Step 2: Choose type for pass status
Pass/fail is a true/false value, soboolfits perfectly.Final Answer:
char for grade, bool for pass status -> Option BQuick Check:
Letter = char, true/false = bool [OK]
- Using int or float for letter grades
- Using char for true/false values
- Mixing data types incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
