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
#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
#include <iostream>
using namespace std;
int main() {
cout << "Hello, World!" << endl;
cout << "Welcome to C++ programming." << endl;
return 0;
}5. Output
Hello, World!
Welcome to C++ programming.6. Key Takeaways
- The classic first program prints
Hello, World!usingcoutand the insertion operator<<. #include <iostream>is required to usecout/cinfor console input and output.main()is the mandatory starting point of execution and must return anint.endlinserts a newline and flushes the output buffer after printing.
Practice what you learned
1. Which header must be included to use `cout` in a C++ program?
2. What operator is used to send output to `cout`?
3. What does `endl` do when used with `cout`?
4. What is the return type of the `main()` function in the Hello World example?
5. What would `cout << "Hello, World!";` output on the console (ignoring newline)?
Was this page helpful?
You May Also Like
Structure of a C++ Program
Understand the essential building blocks of a C++ program — headers, namespaces, the main function, and statements — with an annotated example.
Compilation Process in C++
Follow the full journey of a C++ program from source code to executable, covering preprocessing, compilation, assembly, and linking stages.
cin and cout in C++
Learn how `cin` and `cout` handle standard input and output in C++ using the stream extraction and insertion operators, with chaining and gotchas.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics