Hello World Program
The classic first program in C
The "Hello World" program is the first step towards learning any programming language. It is one of the simplest programs that introduces aspiring programmers to the programming language. It typically outputs the text "Hello, World!" to the console screen.
🤔 Why Start with Hello World?
- •Verifies your compiler and development environment are set up correctly
- •Teaches the basic structure of a C program
- •Introduces fundamental concepts: headers, main function, and output
- •Builds confidence before tackling complex programs
C Program to Print Hello World
To print Hello World, we use the printf function from the stdio.h library that prints the given string on the screen.
1// Header file for input output functions2#include <stdio.h>34// Main function: entry point for execution5int main() {67 // Writing print statement to print hello world8 printf("Hello World");910 return 0;11}Hello World
Detailed Code Explanation
#include <stdio.h>This line includes the Standard Input-Output library in the program. The stdio.h header file contains declarations for:
- •
printf()- for output to screen - •
scanf()- for input from keyboard - • File handling functions
💡 Note: The #include is a preprocessor directive - it tells the compiler to copy the contents of stdio.h into your program before compilation.
int main()The main function is where the execution of the program begins. Every C program must have exactly one main function.
intReturn type - the function returns an integer value
mainFunction name - required entry point
()Empty parentheses - no parameters
{ }Curly braces - contain function body
printf("Hello World");This function call prints Hello World to the console screen.
printf() Function Signature:
int printf(const char *format, ...);- • format: The string to print (can include format specifiers)
- • ...: Optional additional arguments for format specifiers
- • Returns: Number of characters printed
⚠️ Important: Every statement in C ends with a semicolon (;). Forgetting it causes a compilation error!
return 0;This statement indicates that the program ended successfully. The value 0 is returned to the operating system.
| Return Value | Meaning |
|---|---|
| 0 | Program executed successfully |
| Non-zero | Program encountered an error |
Step-by-Step Execution:
The #include directive copies contents of stdio.h header file into your program
Compiler translates your C code into machine code (object file)
Linker combines your object file with library code (printf implementation)
Operating system calls the main() function
The string "Hello World" is sent to the standard output (console)
return 0 tells OS the program completed successfully
How to Compile and Run
🐧 Linux/Mac (GCC)
1# Compile2gcc hello_world.c -o hello34# Run5./hello🪟 Windows (MinGW)
1# Compile2gcc hello_world.c -o hello.exe34# Run5hello.exeVariations of Hello World
With Newline Character
1printf("Hello World\n"); // Adds a new line after output\\n is an escape sequence that moves cursor to the next line.
Using puts() Function
1puts("Hello World"); // Automatically adds newlineputs() is simpler for plain strings and adds a newline automatically.
Multiple Print Statements
1printf("Hello ");2printf("World");3printf("!\n");4// Output: Hello World!⚠️ Common Mistakes to Avoid
- • Missing semicolon:
printf("Hello")❌ should beprintf("Hello");✓ - • Wrong quotes: Using curly quotes instead of straight quotes
- • Missing header: Forgetting
#include <stdio.h> - • Case sensitivity:
PrintforPRINTFwill not work - useprintf
📝 Key Takeaways
main() functionstdio.h provides input/output functionsprintf() displays text on screenreturn 0 indicates successful executionRelated Examples
Want to Learn More?
Explore our comprehensive tutorials for in-depth explanations of C programming concepts.
Browse Tutorials