First Program – Hello World
Your First C Program
The tradition in programming is that the first program you write in any language prints "Hello, World!" on the screen. It tests that your development environment is set up correctly and shows you the basic structure of a program.
#include <stdio.h> int main() { printf("Hello, World!\n"); return 0; }
Line-by-Line Explanation
Line 1: #include <stdio.h>
This is a preprocessor directive. It tells the compiler to include the contents of the stdio.h (Standard Input Output) header file. This file contains the definition of printf() and scanf(). Without this line, printf would be unknown to the compiler.
Line 2: int main() { }
This is the main function — the entry point of every C program. When you run a C program, execution always starts from main(). The int before main means the function returns an integer value.
Line 3: printf("Hello, World!\n");
printf() is a library function that prints text to the screen. The text inside double quotes is called a string literal. \n is an escape sequence that means "newline" — it moves the cursor to the next line after printing.
Line 4: return 0;
The return 0; statement returns the value 0 to the operating system, indicating that the program ran successfully. A non-zero return value typically indicates an error.
How to Compile and Run
# Step 1: Save your file as hello.c # Step 2: Compile gcc hello.c -o hello # Step 3: Run (Linux/Mac) ./hello # Step 3: Run (Windows) hello.exe
Variations
#include <stdio.h> int main() { printf("Welcome to CodingEasily!\n"); printf("Learning C Programming\n"); printf("Beginner to Advanced\n"); return 0; }
Common Errors in First Program
| Error | Cause | Fix |
|---|---|---|
| error: stdio.h not found | Missing #include | Add #include <stdio.h> |
| warning: implicit declaration of printf | Missing stdio.h | Add #include <stdio.h> |
| Missing ; before } | Forgot semicolon | Add ; after printf statement |
| undefined reference to main | No main function | Write int main() { } |
Summary
- Every C program starts execution from
main() #include <stdio.h>is needed to use printf and scanf- Statements end with semicolon
; - Code blocks are enclosed in curly braces
{ } printf()displays text,\nmoves to next linereturn 0;signals successful program completion
Frequently Asked Questions
How do you write a Hello World program in C?
#include <stdio.h>, write a main function, and call printf("Hello, World!"); inside it, ending with return 0;. Compiling and running that program prints the text on the screen.What does #include mean in C?
printf and scanf. Without this line, the program would not know what printf is and would fail to compile.What is the main function in C?
main is the starting point of every C program — execution always begins there. The int before it means it returns an integer to the operating system, and return 0; signals that the program finished successfully.Why is return 0 written at the end of main?
return 0; tells the operating system that the program ended without errors. A zero conventionally means success, while a non-zero value would indicate that something went wrong during execution.How do you compile and run a C program?
.c file, compile it with a compiler such as gcc — for example gcc hello.c -o hello — and then run the resulting program with ./hello. Online compilers can do the same without installing anything.