Skip to main content

First C++ Program

Setting up the Environment: Your First C++ Program

C++ is a versatile programming language used for various purposes, including system software, game development, and desktop applications. This guide will help you set up the environment and run your first C++ program.

Installing a C++ Compiler

The first step is to install a C++ compiler. The compiler translates your C++ code into machine language that your computer can understand. Here are some popular options:

  • GCC: The GNU Compiler Collection is a compiler for Linux and MacOS. It can be downloaded from the GCC website.
  • MinGW: Minimalist GNU for Windows is a port of GCC for the Windows operating system. You can download it from the MinGW website.
  • Visual Studio: Microsoft’s Integrated Development Environment (IDE) contains a C++ compiler for Windows. You can download it from the Visual Studio website.

Install the compiler according to the instructions on the respective websites.

Setting up a Text Editor

Next, you need a text editor to write your C++ code. While you can use any text editor, some are specifically designed for coding and include helpful features. Here are some recommendations:

  • Visual Studio Code: A free and open-source code editor that supports C++ and many other languages. It can be downloaded from the Visual Studio Code website.
  • Sublime Text: A sophisticated text editor for code with a slick user interface. It can be downloaded from the Sublime Text website.
  • Atom: A free and open-source text editor that is modern, approachable, and hackable to its core. It can be downloaded from the Atom website.

Writing Your First C++ Program

Open your text editor and create a new file. Name the file HelloWorld.cpp. In this file, type the following code:

#include <iostream>

int main() {
std::cout << "Hello, World!";
return 0;
}

This is a simple C++ program that prints "Hello, World!" to the console.

  • #include <iostream> is a preprocessor directive that includes the iostream library.
  • int main() is the main function where the program starts execution.
  • std::cout << "Hello, World!"; is used to print "Hello, World!" on the screen.
  • return 0; ends the main function and returns the value 0.

Compiling and Running Your Program

Save your file and open the terminal or command prompt. Navigate to the directory containing your HelloWorld.cpp file.

To compile the program, type the following command and press enter:

  • If you are using GCC or MinGW, type g++ HelloWorld.cpp -o HelloWorld
  • If you are using Visual Studio, type cl /EHsc HelloWorld.cpp

This should create an executable file. To run the program, type the following command and press enter:

  • If you are using GCC or MinGW, type ./HelloWorld
  • If you are using Visual Studio, type HelloWorld

You should see "Hello, World!" printed to the console. Congratulations, you have successfully written, compiled, and run your first C++ program!

Remember to practice coding regularly. The best way to learn is by doing. Happy coding!