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.
Frequently Asked Questions
How do you write a Hello World program in C++?
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++?
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++?
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++?
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++ में:
#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;सफल समाप्ति दर्शाता है।