🟢 Beginner  ·  Lesson 04

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.

hello.c – Your First C Program
#include <stdio.h>

int main() {
    printf("Hello, World!\n");
    return 0;
}
Hello, World!

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

Terminal / Command Prompt
# 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

C Language – Multiple Lines
#include <stdio.h>
int main() {
    printf("Welcome to CodingEasily!\n");
    printf("Learning C Programming\n");
    printf("Beginner to Advanced\n");
    return 0;
}
Welcome to CodingEasily! Learning C Programming Beginner to Advanced

Common Errors in First Program

ErrorCauseFix
error: stdio.h not foundMissing #includeAdd #include <stdio.h>
warning: implicit declaration of printfMissing stdio.hAdd #include <stdio.h>
Missing ; before }Forgot semicolonAdd ; after printf statement
undefined reference to mainNo main functionWrite 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, \n moves to next line
  • return 0; signals successful program completion

Frequently Asked Questions

How do you write a Hello World program in C?
You include the standard input-output header with #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?
It tells the compiler to include the Standard Input Output library, which provides functions like 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?
You save the code in a .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.
← Back to C Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.