Your First C++ Program

Now that you have installed a C++ compiler and set up your development environment, you are ready to write your first C++ program.

The traditional first program in most programming languages is the Hello World program. It simply prints a message on the screen.

Example Program

#include <iostream>
using namespace std;

int main() {

    cout << "Hello, World!";

    return 0;
}

Explanation of the Program

  • #include <iostream> – This line tells the compiler to include the input-output stream library. It allows us to use cout for printing output.
  • using namespace std; – This allows us to use standard library names like cout without writing std:: every time.
  • int main() – This is the main function where the program starts execution.
  • cout << “Hello, World!”; – This statement prints text to the screen.
  • return 0; – This ends the program and indicates that it ran successfully.

Compile and Run the Program

After writing the program, you need to compile and run it.

Compile the program using the following command:

g++ program.cpp -o program

Run the compiled program:

./program

Output

Hello, World!

Next Topic

Next, learn about C++ Syntax.