Hello, World!
Writing your first C++ program.
Table of Contents
In this lesson, we will write our very first C++ program: "Hello, World!". This is a classic program that simply prints "Hello, World!".
#include <bits/stdc++.h>
using namespace std;
int main() {
cout << "Hello, World!" << endl;
return 0;
}
Compile and run this code. You should see the following output:
Hello, World!
Breaking It Down
#include <bits/stdc++.h>
using namespace std;
These two lines enable us to use anything from the C++ standard library.
int main() {
This line marks the point from where the program starts running, the beginning of the main function. Functions discusses in detail about this, for now let's just call it "the main function."
cout << "Hello, World!" << endl;
This line is a statement, and it instructs the program to print Hello, World! and end the line with endl.
Here, "Hello, World!" is a string literal. A literal is a value that is specified directly in the code, and a string literal is a piece of text enclosed by double quotes (").
Note the semicolon (;), each C++ statement must end with a semicolon. Beginners, even seasoned programmer often miss out the semicolon which results in the program failing to compile, so keeping the semicolon in mind saves a huge amount of debugging time and frustration.
return 0;
This line is also a statement and instructs the program to stop. The number 0 indicates the program has succesfully performed its job.
}
Marks the end of the main function.