Writing your First C++ Program

Writing and running your first C++ program is a simple and rewarding process. Let’s walk through creating a “Hello, removeload.com!” program, which is a traditional first program in any language. Below, I’ll guide you through writing the code, compiling it, and running it on your system.

Step 1: Write Your First C++ Program

1. Create a new file:

Create a new file and name it main.cpp.

2. Write the code: Copy and paste the following C++ code into your main.cpp file:


#include <iostream>  // Preprocessor directive to include input-output stream

int main() {  // Main function where program execution begins
    std::cout << "Hello, removeload.com!" << std::endl;  // Prints the message to the console
    return 0;  // Indicates successful completion of the program
}

Explanation:

  • include<iostream>: This line includes the input-output stream library that allows us to use std::cout to output text to the screen.
  • int main(): This is the main function where the program starts execution. Every C++ program must have a main() function.
  • std::cout: This is used to output (print) data to the console. It’s part of the C++ Standard Library.
  • <<: This is the insertion operator that sends the string "Hello, removeload.com!" to std::cout.
  • std::endl: This adds a new line after the message is printed.
  • return 0;: This indicates that the program has finished successfully.

Step 2: Compile and Run the Program

Now that you have your code ready, let's go through the steps of compiling and running it.

On Windows (Using MinGW or MSVC)

1. Open Command Prompt:

Press Windows Key + R, type cmd, and press Enter to open the Command Prompt.

2. Navigate to your program's directory: Use the cd command to navigate to the directory where you saved main.cpp. For example:


cd C:\path\to\your\file

3. Compile the program: If you're using MinGW (GCC), you can compile the code using g++. Run the following command:


g++ main.cpp -o hello
  1. This tells the compiler to compile main.cpp and create an executable called hello.exe.
  2. If you're using MSVC (Visual Studio), you can open the Developer Command Prompt for Visual Studio and use the cl command:

cl main.cpp

4. Run the program:

If you used MinGW


./hello

If you used MSVC


hello.exe

Output:

Hello, removeload.com!