100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
C++

Your First C++ Program (Hello World)

Write, compile, and run your first C++ program — the classic Hello World example — with a line-by-line explanation of every part.

Introduction to C++Beginner5 min readJul 7, 2026
Analogies

1. Intro

The traditional way to start learning any programming language is to write a program that displays Hello, World! on the screen. It is simple enough to focus on the language's basic structure rather than complex logic, while still touching every essential building block of a real C++ program.

🏏

Cricket analogy: Writing 'Hello, World!' is like a young cricketer's first net session hitting straight drives before facing a real bowler — simple enough to focus purely on batting stance and grip rather than complex match strategy, while still touching every essential fundamental.

2. Syntax

cpp
#include <iostream>
using namespace std;

int main() {
    cout << "Hello, World!" << endl;
    return 0;
}

3. Explanation

The line #include <iostream> tells the preprocessor to include the input/output stream library, which defines cout (console output) and cin (console input). using namespace std; lets us write cout directly instead of std::cout, since cout belongs to the std namespace. int main() { ... } defines the mandatory entry-point function; the int before it means main() returns an integer value to the operating system. Inside the body, cout << "Hello, World!" << endl; sends the text "Hello, World!" to the standard output stream, and the << (insertion operator) directs data into the stream; endl inserts a newline and flushes the output buffer. Finally, return 0; ends main() and reports successful completion to the operating system.

🏏

Cricket analogy: #include <iostream> is like requesting the official scoring toolkit before a match (defining cout for announcing runs and cin for recording input); using namespace std; is like agreeing to call it just 'the scorebook' instead of the full 'ICC official scorebook' every time; int main() is the mandatory toss that starts every match and must report a result code back to the board; cout << "Hello, World!" << endl; is like the announcer's PA system sending text to the crowd, with << directing the message and endl like the pause before the next announcement; and return 0; is the official confirmation that the match concluded successfully.

4. Example

cpp
#include <iostream>
using namespace std;

int main() {
    cout << "Hello, World!" << endl;
    cout << "Welcome to C++ programming." << endl;
    return 0;
}

5. Output

text
Hello, World!
Welcome to C++ programming.

6. Key Takeaways

  • The classic first program prints Hello, World! using cout and the insertion operator <<.
  • #include <iostream> is required to use cout/cin for console input and output.
  • main() is the mandatory starting point of execution and must return an int.
  • endl inserts a newline and flushes the output buffer after printing.

Practice what you learned

Was this page helpful?

Topics covered

#CStudyNotes#Programming#YourFirstCProgramHelloWorld#Program#Hello#World#Syntax#StudyNotes#SkillVeris#ExamPrep