🟢 Foundation  ·  Lesson 04

First C++ Program: Hello World

The Hello World program

The tradition in every language is to make your first program print a greeting. Here it is in C++:

hello.cpp
#include <iostream>
using namespace std;

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

Line-by-line explanation

LineWhat it does
#include <iostream>Brings in the input/output library so you can use cout.
using namespace std;Lets you write cout instead of std::cout.
int main() {The starting point; every C++ program has exactly one main.
cout << "Hello, World!" << endl;Prints the text and moves to a new line.
return 0;Tells the system the program finished successfully.

How cout prints

cout is the "console output" stream. The << operator "sends" whatever is on its right into the output. You can chain several sends in one line:

C++
cout << "Sum = " << 2 + 3 << endl;
Output:
Sum = 5

Text goes in quotes; numbers and calculations do not. C++ works out 2 + 3 and prints the result.

Compiling and running

Save the program as hello.cpp and, in a terminal:

Terminal
g++ hello.cpp -o hello
./hello

If you have not set up a compiler yet, see the C++ setup lesson, or use an online compiler to run it instantly.

Small variations to try

C++
cout << "Line one" << endl;
cout << "Line two\n";        // \n also starts a new line
cout << "No newline here";
Output:
Line one
Line two
No newline here

Common errors

  • Forgetting the semicolon at the end of a statement.
  • Leaving out #include <iostream>, so cout is undefined.
  • Using Cout or COUT — C++ is case-sensitive, it must be cout.
  • Writing cout >> instead of cout << (the arrows point toward cout for input, away for output).
🏋️ Practice

Change the program to print your name on one line and your favourite language on the next. Then print the result of 10 * 5 with a label like "Answer = ".

Summary

  • A C++ program needs #include <iostream> and a main function.
  • cout << ... prints text and values; endl or \n starts a new line.
  • Text goes in quotes; numbers and math do not.
  • Every statement ends with a semicolon, and C++ is case-sensitive.
  • return 0; signals a successful finish.

Frequently Asked Questions

How do you write a Hello World program in C++?
You include the iostream header, use the std namespace, write a main function, and print with cout. The full program is #include <iostream>, using namespace std;, then int main() { cout << "Hello, World!"; return 0; }.
What does #include do in C++?
It includes the input/output stream library, which provides cout for printing and cin for reading input. Without this header the compiler would not recognise cout, so nearly every beginner program starts with it.
What does using namespace std mean in C++?
The standard library names live in a namespace called std. Writing using namespace std; lets you use names like cout and endl directly instead of writing std::cout every time. It is convenient for learning, though large projects often prefer the explicit std:: form.
What is cout in C++?
cout is the standard output stream that prints text and values to the screen. You send data to it with the insertion operator <<, for example cout << "Hi" << 5;, which prints Hi5.
What is the difference between endl and \n in C++?
Both move output to a new line. endl also flushes the output buffer, which can be slightly slower, while "\n" just adds a newline character. For simple programs the difference does not matter; in performance-sensitive code many prefer "\n".

Hello World program

हर language में परंपरा है कि आपका पहला program एक अभिवादन print करे। यह रहा C++ में:

hello.cpp
#include <iostream>
using namespace std;

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

Line-by-line व्याख्या

Lineयह क्या करती है
#include <iostream>Input/output library लाती है ताकि आप cout इस्तेमाल कर सकें।
using namespace std;आपको std::cout के बजाय cout लिखने देती है।
int main() {शुरुआती बिंदु; हर C++ program में ठीक एक main होता है।
cout << "Hello, World!" << endl;Text print करती है और नई line पर जाती है।
return 0;System को बताती है कि program सफलतापूर्वक समाप्त हुआ।

cout कैसे print करता है

cout "console output" stream है। << operator अपने दाईं ओर जो हो उसे output में "भेजता" है। आप एक line में कई भेज chain कर सकते हैं:

C++
cout << "Sum = " << 2 + 3 << endl;
Output:
Sum = 5

Text उद्धरण चिह्नों में जाता है; numbers और गणनाएँ नहीं। C++ 2 + 3 हल करके परिणाम print करता है।

Compile और run करना

Program को hello.cpp के रूप में सहेजें और, terminal में:

Terminal
g++ hello.cpp -o hello
./hello

अगर आपने अभी compiler set up नहीं किया, C++ setup lesson देखें, या इसे तुरंत चलाने को online compiler इस्तेमाल करें।

आज़माने को छोटे बदलाव

C++
cout << "Line one" << endl;
cout << "Line two\n";        // \n bhi nai line shuru karta hai
cout << "No newline here";
Output:
Line one
Line two
No newline here

आम errors

  • Statement के अंत में semicolon भूलना।
  • #include <iostream> छोड़ देना, तो cout undefined हो जाता है।
  • Cout या COUT इस्तेमाल करना — C++ case-sensitive है, यह cout ही होना चाहिए।
  • cout << के बजाय cout >> लिखना (तीर input के लिए cout की ओर, output के लिए दूर इशारा करते हैं)।
🏋️ अभ्यास

Program बदलें ताकि यह एक line पर आपका नाम और अगली पर आपकी पसंदीदा language print करे। फिर 10 * 5 का परिणाम "Answer = " जैसे label के साथ print करें।

सारांश

  • C++ program को #include <iostream> और एक main function चाहिए।
  • cout << ... text और values print करता है; endl या \n नई line शुरू करता है।
  • Text उद्धरण चिह्नों में जाता है; numbers और गणित नहीं।
  • हर statement semicolon से खत्म होता है, और C++ case-sensitive है।
  • return 0; सफल समाप्ति दर्शाता है।
← 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.