Overview
Writing your first C++ program is an exciting step into the world of software development. In this post we will guide you through the process of writing and running your first C++ program.
Step 1: Setting Up Your Environment
Before you can start coding, you need a development environment. Here's what you will need:
A C++ Compiler:
You can use popular C++ compilers like GCC (GNU Compiler Collection), Clang or Microsoft Visual C++ depending on your operating system. For this course I will be using the GCC in Ubuntu linux.
Installing GCC via Command Line in Ubuntu:
- Open a terminal window. You can do this by pressing
Ctrl+Alt+T
or by searching forTerminal
in the Ubuntu application launcher. - Update the package list to ensure you have the latest information about available packages and their version:
sudo apt update
- Install GCC by running the following command:
sudo apt install gcc
or installing GCC for C++ development (this will install GCC and g++ both):sudo apt install g++
or installing additional tools which includes essential build-tools, libraries, GCC and g++.sudo apt install build-essential
- During the installation, Ubuntu may ask for your password to confirm that you have the necessary privileges. Enter you password and press
Enter
to continue. - Once the installation is complete, you can verifiy the installed version of GCC by running:
gcc --version
This command will display the installed GCC version.
A Text Editor or Integrated Development Environment (IDE):
You can use a simple text editor or a code-focused IDE like Visual Studio Code.
Step 2: Writing Your First C++ Program
Let's start with quintessential “Hello, World” program. This simple program displays the text “Hello, World” on your screen.
#include <iostream>
int main() {
std::cout << "Hello, World!" << std::endl;
return 0;
}
Here's what happening in this program:
- “#include <iostream>”: This line includes the
iostream
library, which allows us to use input and output functions. - “int main()”: Every C++ program has a
main
function. It's where the program start executing. - “{ }”: These curly braces enclose the body of the
main
function, where you put your code. - 'std::cout << “Hello, World” << std::endl;': This line used
std::cout
to printHello, World
to the console.std::endl
adds a newline character, so that next cout statement start printing from next line. - “return 0;”: This line indicates that the program has executed successfully. The
0
means it exited without errors.
Step 3: Compiling and Running Your Program
Now that you have written your code, it's time to compile and run it.
- Save your file with a
.cpp
extension, for example,hello.cpp
. - Open your terminal or command prompt and navigate to the directory where your file is located.
- Compile your program using the C++ compiler. For example, as we installed g++, so we be using it:
g++ hello.cpp -o hello
This command complieshello.cpp
and generates an executable file namedhello
. - Run your program:
./hello
You should see the output:
Hello, World