🚀Beginner

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.

hello_world.c
C
1// Header file for input output functions
2#include <stdio.h>
3
4// Main function: entry point for execution
5int main() {
6
7 // Writing print statement to print hello world
8 printf("Hello World");
9
10 return 0;
11}
Output

Hello World

Detailed Code Explanation

Line 2#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.

Line 5int main()

The main function is where the execution of the program begins. Every C program must have exactly one main function.

int

Return type - the function returns an integer value

main

Function name - required entry point

()

Empty parentheses - no parameters

{ }

Curly braces - contain function body

Line 8printf("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!

Line 10return 0;

This statement indicates that the program ended successfully. The value 0 is returned to the operating system.

Return ValueMeaning
0Program executed successfully
Non-zeroProgram encountered an error

Step-by-Step Execution:

1
Preprocessor Phase

The #include directive copies contents of stdio.h header file into your program

2
Compilation

Compiler translates your C code into machine code (object file)

3
Linking

Linker combines your object file with library code (printf implementation)

4
Execution Starts

Operating system calls the main() function

5
printf() Executes

The string "Hello World" is sent to the standard output (console)

6
Program Exits

return 0 tells OS the program completed successfully

How to Compile and Run

🐧 Linux/Mac (GCC)

main.c
C
1# Compile
2gcc hello_world.c -o hello
3
4# Run
5./hello

🪟 Windows (MinGW)

main.c
C
1# Compile
2gcc hello_world.c -o hello.exe
3
4# Run
5hello.exe

Variations of Hello World

With Newline Character

main.c
C
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

main.c
C
1puts("Hello World"); // Automatically adds newline

puts() is simpler for plain strings and adds a newline automatically.

Multiple Print Statements

main.c
C
1printf("Hello ");
2printf("World");
3printf("!\n");
4// Output: Hello World!

⚠️ Common Mistakes to Avoid

  • Missing semicolon: printf("Hello") ❌ should be printf("Hello");
  • Wrong quotes: Using curly quotes instead of straight quotes
  • Missing header: Forgetting #include <stdio.h>
  • Case sensitivity: Printf or PRINTF will not work - use printf

📝 Key Takeaways

Every C program needs a main() function
stdio.h provides input/output functions
printf() displays text on screen
return 0 indicates successful execution
Statements end with semicolons
C is case-sensitive

Want to Learn More?

Explore our comprehensive tutorials for in-depth explanations of C programming concepts.

Browse Tutorials
Back to All Examples