Writing first C program - Time & Space Complexity
When writing your first C program, it is helpful to understand how the program's steps grow as the input changes.
We want to see how the time the program takes changes when we run it with different inputs.
Analyze the time complexity of the following code snippet.
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
This code prints a simple greeting message once and then ends.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Printing the message once.
- How many times: Exactly one time.
Since the program prints the message only once, the time it takes does not change with input size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 |
| 100 | 1 |
| 1000 | 1 |
Pattern observation: The number of operations stays the same no matter the 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: "The program takes longer if I run it on a bigger input or with more input."
[OK] Correct: This program does the same single action every time, so input size or computer speed does not change how many steps it takes.
Understanding that some programs run in constant time helps you explain how simple tasks behave, which is a useful skill in coding interviews and real projects.
"What if we added a loop to print the message n times? How would the time complexity change?"
Practice
int main() function in a C program?Solution
Step 1: Understand the role of
Themain()main()function is where the program starts running in C.Step 2: Identify the correct purpose
It is not for declaring variables or printing messages but for starting the program.Final Answer:
It is the starting point where the program begins execution. -> Option BQuick Check:
Program starts atmain()[OK]
- Thinking
main()prints output - Confusing
main()with variable declaration - Believing
main()ends the program immediately
Solution
Step 1: Identify the correct function for output
In C,printfis used to print text to the screen.Step 2: Check syntax details
Each statement must end with a semicolon;. Soprintf("Hello, World!");is correct.Final Answer:
printf("Hello, World!"); -> Option AQuick Check:
Useprintfwith semicolon [OK]
printf with a semicolon [OK]- Missing semicolon at the end
- Using
printinstead ofprintf - Using
echowhich is not C syntax
#include
int main() {
printf("Welcome to C programming!\n");
return 0;
}Solution
Step 1: Understand the
The string insideprintfstatementprintfincludes \n which means new line after printing.Step 2: Predict the output
The program prints "Welcome to C programming!" followed by a new line.Final Answer:
Welcome to C programming! -> Option DQuick Check:
\n creates new line, output is plain text [OK]
- Thinking \n prints as text
- Expecting syntax error due to \n
- Ignoring the return statement effect
#include
int main() {
printf("Hello World")
return 0;
}Solution
Step 1: Check each statement for syntax
Theprintfstatement is missing a semicolon at the end.Step 2: Verify other parts
Themainfunction has correct return type and header file is correct.Final Answer:
Missing semicolon after printf statement -> Option CQuick Check:
Every statement ends with ; [OK]
- Ignoring missing semicolon errors
- Thinking return type is missing
- Assuming wrong header file causes error
Solution
Step 1: Understand how to print two lines
Each line should end with a newline character \n to move to the next line.Step 2: Check each option for correct newlines
#include int main() { printf("Hello!\n"); printf("Welcome to C.\n"); return 0; } prints "Hello!" with \n and "Welcome to C." with \n, so two lines appear.Final Answer:
Two printf statements, each ending with \n -> Option AQuick Check:
Use \n at end of each line for separate lines [OK]
- Missing \n causing lines to join
- Putting \n only on first line
- Not returning 0 at end of main
Ready to prove it?
Pass the test and this pattern moves unknown
