C++ First Program

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 use cout without writing std::.
  • int main() is the starting point of every C++ program.
  • cout displays the text on the screen.
  • return 0; ends the program successfully.

How to Run the Program

  1. Open your C++ editor or IDE.
  2. Create a new file.
  3. Copy and paste the above code.
  4. Save the file with the .cpp extension.
  5. Click Run or press the run button in your IDE.
  6. The output Hello, World! will appear on the screen.

Key Points

  • Every C++ program starts from the main() function.
  • cout is used to display output.
  • Every statement ends with a semicolon (;).
  • Save C++ files with the .cpp extension.

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top