Your First C Program: "Hello, World!"
When learning C programming, the “Hello, World!” program is traditionally your first step. It’s simple, easy to understand, and teaches the basic structure of a C program.
What Is the "Hello, World!" Program?
It’s a basic program that displays the message:
Hello, World!
This program helps you understand the syntax and structure of a C program.
Code: Hello World in C
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
🧪 Step-by-Step Explanation
Line | Explanation |
---|---|
#include <stdio.h> | Preprocessor directive. Includes Standard Input Output library so you can use printf() |
int main() | The main function – where the program starts running |
{ ... } | Curly braces define the body of the function |
printf("Hello, World!\n"); | Prints text to the screen. \n means new line |
return 0; | Ends the program and returns control to the OS |
🛠️ How to Run Hello World (Step-by-Step)
✅ On Windows (using Code::Blocks)
Open Code::Blocks
Create a new project → Console Application → Select C
Paste the above code
Click Build and Run
✅ On Linux / macOS (using terminal)
Open a terminal
Create a file:
nano hello.cPaste the code
Compile:
gcc hello.c -o helloRun:
./hello
✅ Output
Hello, World!
🎉 Congratulations — You’ve written your first C program!
🧠 Best Practices
Always include comments in your code
Keep code indented and clean
Use
return 0;
at the end ofmain()
to indicate successful executionUse meaningful output messages as you progress
❌ Common Mistakes to Avoid
Mistake | Solution |
---|---|
Missing semicolon | Add ; at the end of printf() |
Using wrong quotes | Use " " not ' ' for strings |
Typo in printf | Make sure spelling is correct |
Forgetting #include line | Required to use printf() |