Writing Your First C++ Program

Once the environment is ready, you can write your first C++ program.

General Syntax:

#include <iostream>

using namespace std;

int main() {

    // Statements

    return 0;

}

Example:

#include <iostream>

using namespace std;

int main() {

    cout << “Hello, C++ World!” << endl;

    return 0;

}

Output:

Hello, C++ World!

Compiling and Running a C++ Program

The way you compile and run your code depends on the environment you’re using:

  1. a) Using GCC in Command Line (Linux or MinGW on Windows)
  1. Save the code in a file named program.cpp.
  2. Open the terminal or Command Prompt.
  3. Compile using:  g++ program.cpp -o program
  4. Run the compiled file:
    • On Linux/macOS: ./program
    • On Windows: program.exe
  1. b) Using an IDE (e.g., Code::Blocks or Visual Studio)
  1. Open the IDE.
  2. Create a new C++ project.
  3. Paste your code into the editor.
  4. Click Build or Compile, then Run.