Bird
Raised Fist0
Cprogramming~10 mins

Writing first C program - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Writing first C program
Start
↓
Include stdio.h
↓
Define main() function
↓
Execute printf statement
↓
Return 0
↓
End program
This flow shows the steps of a simple C program: include library, define main, print message, return success, and end.
Execution Sample
C
#include <stdio.h>

int main() {
    printf("Hello, world!\n");
    return 0;
}
This program prints 'Hello, world!' to the screen and then ends.
Execution Table
StepCode LineActionOutputNext Step
1#include <stdio.h>Include standard input/output library2
2int main() {Start main function3
3printf("Hello, world!\n");Print text to screenHello, world!4
4return 0;Return 0 to signal success5
5}End main functionProgram ends
💡 Program ends after main returns 0, signaling successful completion.
Variable Tracker
VariableStartAfter printfFinal
None---
Key Moments - 3 Insights
Why do we include <stdio.h> at the start?
Because printf is defined in stdio.h, including it allows us to use printf without errors, as shown in step 1 of the execution_table.
What does 'return 0;' mean in main()?
It tells the system the program finished successfully, as seen in step 4 where return 0 ends the program.
Why is there a \n inside the printf string?
The \n adds a new line after printing, so the output looks clean, demonstrated in step 3 where 'Hello, world!' is printed with a line break.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what does step 3 do?
AEnds the program
BPrints 'Hello, world!' to the screen
CIncludes the stdio.h library
DReturns 0 to the system
💡 Hint
Check the 'Action' and 'Output' columns in step 3 of the execution_table.
At which step does the program signal successful completion?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look for 'Return 0' in the 'Code Line' and 'Action' columns.
If we remove '#include <stdio.h>', what will happen?
ACompiler will give an error about printf
BProgram will print 'Hello, world!' normally
CProgram will return 1 instead of 0
DProgram will run but output nothing
💡 Hint
Recall why stdio.h is included from the key_moments about step 1.
Concept Snapshot
#include <stdio.h>  // Include input/output library
int main() {          // Main function start
  printf("Hello, world!\n");  // Print message
  return 0;           // End program successfully
}
Full Transcript
This simple C program starts by including the standard input/output library stdio.h so we can use printf. The main function is where the program begins running. Inside main, printf prints the message 'Hello, world!' followed by a new line. Then return 0 signals the program finished successfully. The program ends after main returns. Each step is shown in the execution table, helping beginners see how the program runs line by line.

Practice

(1/5)
1. What is the purpose of the int main() function in a C program?
easy
A. It prints messages to the screen.
B. It is the starting point where the program begins execution.
C. It declares variables for the program.
D. It ends the program immediately.

Solution

  1. Step 1: Understand the role of main()

    The main() function is where the program starts running in C.
  2. Step 2: Identify the correct purpose

    It is not for declaring variables or printing messages but for starting the program.
  3. Final Answer:

    It is the starting point where the program begins execution. -> Option B
  4. Quick Check:

    Program starts at main() [OK]
Hint: Remember: C programs always start at main() [OK]
Common Mistakes:
  • Thinking main() prints output
  • Confusing main() with variable declaration
  • Believing main() ends the program immediately
2. Which of the following is the correct syntax to print "Hello, World!" in C?
easy
A. printf("Hello, World!");
B. printf("Hello, World!")
C. print("Hello, World!");
D. echo("Hello, World!");

Solution

  1. Step 1: Identify the correct function for output

    In C, printf is used to print text to the screen.
  2. Step 2: Check syntax details

    Each statement must end with a semicolon ;. So printf("Hello, World!"); is correct.
  3. Final Answer:

    printf("Hello, World!"); -> Option A
  4. Quick Check:

    Use printf with semicolon [OK]
Hint: Always end printf with a semicolon [OK]
Common Mistakes:
  • Missing semicolon at the end
  • Using print instead of printf
  • Using echo which is not C syntax
3. What will be the output of the following C program?
#include 
int main() {
    printf("Welcome to C programming!\n");
    return 0;
}
medium
A. No output
B. Welcome to C programming!\n
C. Syntax Error
D. Welcome to C programming!

Solution

  1. Step 1: Understand the printf statement

    The string inside printf includes \n which means new line after printing.
  2. Step 2: Predict the output

    The program prints "Welcome to C programming!" followed by a new line.
  3. Final Answer:

    Welcome to C programming! -> Option D
  4. Quick Check:

    \n creates new line, output is plain text [OK]
Hint: \n means new line, not literal text [OK]
Common Mistakes:
  • Thinking \n prints as text
  • Expecting syntax error due to \n
  • Ignoring the return statement effect
4. Identify the error in the following C program:
#include 
int main() {
    printf("Hello World")
    return 0;
}
medium
A. Incorrect header file included
B. Missing return type for main
C. Missing semicolon after printf statement
D. Missing main function

Solution

  1. Step 1: Check each statement for syntax

    The printf statement is missing a semicolon at the end.
  2. Step 2: Verify other parts

    The main function has correct return type and header file is correct.
  3. Final Answer:

    Missing semicolon after printf statement -> Option C
  4. Quick Check:

    Every statement ends with ; [OK]
Hint: Look for missing semicolons after statements [OK]
Common Mistakes:
  • Ignoring missing semicolon errors
  • Thinking return type is missing
  • Assuming wrong header file causes error
5. You want to write a C program that prints two lines: "Hello!" and "Welcome to C." Which code snippet correctly does this?
hard
A. #include int main() { printf("Hello!\n"); printf("Welcome to C.\n"); return 0; }
B. #include int main() { printf("Hello!Welcome to C.\n"); return 0; }
C. #include int main() { printf("Hello!"); printf("Welcome to C.\n"); return 0; }
D. #include int main() { printf("Hello!"); printf("Welcome to C."); return 0; }

Solution

  1. Step 1: Understand how to print two lines

    Each line should end with a newline character \n to move to the next line.
  2. 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.
  3. Final Answer:

    Two printf statements, each ending with \n -> Option A
  4. Quick Check:

    Use \n at end of each line for separate lines [OK]
Hint: Use \n after each line to print on new lines [OK]
Common Mistakes:
  • Missing \n causing lines to join
  • Putting \n only on first line
  • Not returning 0 at end of main
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