🟢 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.

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; सफल समाप्ति दर्शाता है।

अक्सर पूछे जाने वाले प्रश्न (FAQ)

C++ में Hello World program कैसे लिखें?
आप iostream header शामिल करते हैं, std namespace इस्तेमाल करते हैं, एक main function लिखते हैं, और cout से print करते हैं। पूरा program है #include <iostream>, using namespace std;, फिर int main() { cout << "Hello, World!"; return 0; }
C++ में #include क्या करता है?
यह input/output stream library शामिल करता है, जो print करने को cout और input पढ़ने को cin देती है। इस header के बिना compiler cout नहीं पहचानता, तो लगभग हर beginner program इसी से शुरू होता है।
C++ में using namespace std का क्या मतलब है?
Standard library के नाम std नामक namespace में रहते हैं। using namespace std; लिखना आपको cout और endl जैसे नाम सीधे इस्तेमाल करने देता है, हर बार std::cout लिखने के बजाय। सीखने के लिए यह सुविधाजनक है, हालाँकि बड़े projects अक्सर स्पष्ट std:: रूप पसंद करते हैं।
C++ में cout क्या है?
cout standard output stream है जो screen पर text और values print करता है। आप उसे insertion operator << से data भेजते हैं, जैसे cout << "Hi" << 5;, जो Hi5 print करता है।
C++ में endl और \n में क्या अंतर है?
दोनों output को नई line पर ले जाते हैं। endl output buffer को flush भी करता है, जो थोड़ा धीमा हो सकता है, जबकि "\n" बस एक newline character जोड़ता है। सरल programs में अंतर मायने नहीं रखता; performance-संवेदनशील code में कई "\n" पसंद करते हैं।
← Back to C++ Tutorial
🔗

Share this topic with a friend

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

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

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

💻 लाइव कोड एडिटर

इस पेज के प्रोग्राम यहीं तैयार हैं — चलाएँ, बदलें और सीखें। कुछ भी इंस्टॉल किए बिना।
OneCompiler द्वारा संचालित। कोड एडिटर में अपने आप आ जाता है — Run दबाकर आउटपुट देखें। अगर एडिटर न खुले तो नए टैब में खोलें.