Now that you have installed a C++ compiler, it’s time to write your first C++ program. The traditional first program is called “Hello, World!” because it simply displays a message on the screen.
Example
#include <iostream>
using namespace std;
int main() {
cout << "Hello, World!";
return 0;
}
Output
Hello, World!
Code Explanation
#include <iostream>includes the input and output library.using namespace std;allows you to usecoutwithout writingstd::.int main()is the starting point of every C++ program.coutdisplays the text on the screen.return 0;ends the program successfully.
How to Run the Program
- Open your C++ editor or IDE.
- Create a new file.
- Copy and paste the above code.
- Save the file with the
.cppextension. - Click Run or press the run button in your IDE.
- The output Hello, World! will appear on the screen.
Key Points
- Every C++ program starts from the
main()function. coutis used to display output.- Every statement ends with a semicolon (
;). - Save C++ files with the
.cppextension.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.