Loading lessons...
Your First C++ Program
Your First C++ Program
The classic rite of passage in a new language is the "Hello, World!" program. Here's a complete one in C++:
#include <iostream>
using namespace std;
int main() {
cout << "Hello world!" << endl;
return 0;
}
Line by line
#include <iostream>- pulls in the header that provides input and output (likecout).using namespace std;- lets you writecoutinstead ofstd::cout.int main()- the entry point. Every standalone C++ program has exactly onemain(), and execution starts there.cout << "Hello world!" << endl;- sends the text to the console.endlmoves the cursor to a new line.return 0;- reports that the program finished successfully.- The curly braces
{ }define the "block" of code that runs.
A few quick notes
- Each statement ends with a semicolon
;. - The double quotes around
"Hello world!"are part of the command, not decorations. - Your source file is named with a .cpp extension, like
hello.cpp.
Compile and run it
Once you compile and run the program, the console should show:
Hello world!
TL;DR
- Every program has one
main()where execution begins. #include <iostream>gives you input/output tools.cout << ...sends text to the console.return 0;means "all done, successfully".