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++:
#include <iostream>
using namespace std;
int main() {
cout << "Hello, World!" << endl;
return 0;
}Hello, World!
Line-by-line explanation
| Line | What 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:
cout << "Sum = " << 2 + 3 << endl;
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:
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
cout << "Line one" << endl; cout << "Line two\n"; // \n also starts a new line cout << "No newline here";
Line one
Line two
No newline here
Common errors
- Forgetting the semicolon at the end of a statement.
- Leaving out
#include <iostream>, socoutis undefined. - Using
CoutorCOUT— C++ is case-sensitive, it must becout. - Writing
cout >>instead ofcout <<(the arrows point towardcoutfor input, away for output).
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 amainfunction. cout << ...prints text and values;endlor\nstarts 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++ में:
#include <iostream>
using namespace std;
int main() {
cout << "Hello, World!" << endl;
return 0;
}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 कर सकते हैं:
cout << "Sum = " << 2 + 3 << endl;
Sum = 5
Text उद्धरण चिह्नों में जाता है; numbers और गणनाएँ नहीं। C++ 2 + 3 हल करके परिणाम print करता है।
Compile और run करना
Program को hello.cpp के रूप में सहेजें और, terminal में:
g++ hello.cpp -o hello ./hello
अगर आपने अभी compiler set up नहीं किया, C++ setup lesson देखें, या इसे तुरंत चलाने को online compiler इस्तेमाल करें।
आज़माने को छोटे बदलाव
cout << "Line one" << endl; cout << "Line two\n"; // \n bhi nai line shuru karta hai cout << "No newline here";
Line one
Line two
No newline here
आम errors
- Statement के अंत में semicolon भूलना।
#include <iostream>छोड़ देना, तोcoutundefined हो जाता है।CoutयाCOUTइस्तेमाल करना — C++ case-sensitive है, यहcoutही होना चाहिए।cout <<के बजायcout >>लिखना (तीर input के लिएcoutकी ओर, output के लिए दूर इशारा करते हैं)।
Program बदलें ताकि यह एक line पर आपका नाम और अगली पर आपकी पसंदीदा language print करे। फिर 10 * 5 का परिणाम "Answer = " जैसे label के साथ print करें।
सारांश
- C++ program को
#include <iostream>और एकmainfunction चाहिए। cout << ...text और values print करता है;endlया\nनई line शुरू करता है।- Text उद्धरण चिह्नों में जाता है; numbers और गणित नहीं।
- हर statement semicolon से खत्म होता है, और C++ case-sensitive है।
return 0;सफल समाप्ति दर्शाता है।
अक्सर पूछे जाने वाले प्रश्न (FAQ)
C++ में Hello World program कैसे लिखें?
main function लिखते हैं, और cout से print करते हैं। पूरा program है #include <iostream>, using namespace std;, फिर int main() { cout << "Hello, World!"; return 0; }।C++ में #include क्या करता है?
cout और input पढ़ने को cin देती है। इस header के बिना compiler cout नहीं पहचानता, तो लगभग हर beginner program इसी से शुरू होता है।C++ में using namespace std का क्या मतलब है?
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 में क्या अंतर है?
endl output buffer को flush भी करता है, जो थोड़ा धीमा हो सकता है, जबकि "\n" बस एक newline character जोड़ता है। सरल programs में अंतर मायने नहीं रखता; performance-संवेदनशील code में कई "\n" पसंद करते हैं।