Your First C Program
Congratulations! 🎉
In the previous lessons, you learned about the history of C, installed the compiler, and understood the structure of a C program.
Now it's time to write your first C program and execute it successfully.
This is the beginning of your programming journey.
🎯 Learning Objectives
After completing this lesson, you will be able to:
- Write your first C program.
- Understand every line of code.
- Compile and execute a C program.
- Display output using
printf(). - Understand the role of the
main()function. - Identify common beginner mistakes.
📖 What is a C Program?
A C program is a collection of instructions written using the C programming language.
These instructions tell the computer exactly what to do.
Every C program must follow a proper structure and contain a main() function.
💻 Hello World Program
c#include <stdio.h> int main() { printf("Hello, World!"); return 0; }
▶ Output
textHello, World!
🔍 Understanding Every Line
Line 1
c#include <stdio.h>
This includes the Standard Input Output library.
Without it, the compiler doesn't know what printf() is.
Line 2
cint main()
Every C program starts from the main() function.
Execution begins here.
Line 3
c{
Opening brace.
Marks the beginning of the function.
Line 4
cprintf("Hello, World!");
Displays text on the screen.
printf() means Print Formatted.
Line 5
creturn 0;
Returns control back to the operating system.
0 means successful execution.
Line 6
c}
Closing brace.
Marks the end of the function.
🖥 Program Execution
textSource Code │ ▼ Compiler │ ▼ Machine Code │ ▼ Execution │ ▼ Output
📝 Writing Your First Program in VS Code
Step 1
Create a folder
C Programs
Step 2
Create
hello.c
Step 3
Paste the code.
Step 4
Save the file.
Step 5
Compile
bashgcc hello.c -o hello
Step 6
Run
Windows
bashhello
Linux/macOS
bash./hello
Expected Output
textHello, World!
🎨 Customizing the Program
Instead of printing "Hello World",
try
c#include <stdio.h> int main() { printf("Welcome to KAURA Learning Hub!"); return 0; }
Output
textWelcome to KAURA Learning Hub!
Another Example
c#include <stdio.h> int main() { printf("My Name is Kaushal"); return 0; }
Output
textMy Name is Rahul
Printing Multiple Lines
c#include <stdio.h> int main() { printf("Welcome\n"); printf("To\n"); printf("KAURA"); return 0; }
Output
textWelcome To KAURA
Escape Sequences
| Escape Sequence | Meaning |
|---|---|
\n | New Line |
\t | Tab |
\\ | Backslash |
\" | Double Quote |
Compilation Process
texthello.c │ ▼ GCC Compiler │ ▼ hello.exe │ ▼ Output
Common Errors
Missing Semicolon
❌
cprintf("Hello")
✅
cprintf("Hello");
Missing Header File
❌
cint main() { printf("Hello"); }
✅
c#include <stdio.h>
Wrong Main Function
❌
cmain()
✅
cint main()
Missing Curly Braces
Always write
c{ }
Never ignore compiler error messages.
Most errors clearly tell you the line number where the problem occurred.
💡 Best Practices
- Save before compiling.
- Indent your code.
- Use meaningful messages.
- Keep one statement per line.
- Read compiler errors carefully.
Change the program yourself.
Replace "Hello World" with your own name, college, or city and observe the output.
🌍 Real World Analogy
Imagine giving instructions to a robot.
Every instruction must be written correctly.
If one instruction is wrong,
the robot stops working.
Programming works exactly the same way.
🧠 Interview Questions
- What is the first program in C?
- Why do we use
printf()? - What is the purpose of
return 0;? - Why is
main()mandatory? - What is
stdio.h?
✍ Practice Questions
- Print your name.
- Print your college name.
- Print your city.
- Print three lines using
\n. - Modify the Hello World program.
📚 Lesson Summary
You learned:
- How to write your first C program.
- The purpose of every line.
- How to compile and run programs.
- Escape sequences.
- Common errors.
- Best coding practices.
You have now officially written and executed your first C program!
🚀 What's Next?
In the next lesson, Variables in C, you'll learn how computers store data using variables, how to declare them, initialize them, and use them in real programs.
Happy Coding with KauraX 💙