DEV Community

Khandaker Akif Razzak
Khandaker Akif Razzak

Posted on

Journal Entry #2: Hello World! ... The first code

Today, I wrote my first "Hello World" code in C++. This seems to be the first code every developer writes.

Before writing the code, there is a prerequisite. As you see, every code needs to be translated into machine code for computers to understand. The C++ code also needs to be translated into machine code. Compilers do the translating for us.

C++ requires a compiler named GNU compiler collection ... GCC in short. This compiler seems like a common compiler for many programming languages. I had to install it before compiling my Hello_world.cpp file. All the installation and code compilation was done from terminal and the code was written in nano text editor. I performed all the tasks in Ubuntu 20.04.

All the steps to write the Hello World code is as follows:

Step 1:
Fire up a terminal by pressing ctrl+alt+T and install G++ (Linux OS) by typing

$sudo apt install g++

Do not forget to type your password. You will be seeing a bunch of lines running down after that.

Step 2:
Confirm if g++ is installed or not by typing

$g++ --version

You will see an output like this.

g++ (Ubuntu 9.3.0-10ubuntu2) 9.3.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Step 3
Create a new directory in your preferred location and change directory to the one you created. You might have to use sudo to create a directory.

$sudo mkdir hello_world
$cd hello_world

Step 4
Create a text file

$touch hello_world.cpp

Step 5
Edit the file using nano editor. In other words, type nano with the cpp file.

$sudo nano hello_world.cpp

A command-line editor will open. Here you will have to writer your code.

Step 6
Now the moment of truth! You write your first hello world code.

#include <iostream>
using namespace std;
int main(){
     cout<<"Hello World!" <<endl;
     return 0;
}

Step 7
Create a binary file.

$g++ hello_world.cpp -o hello_world

Step 8
Now run the binary file.

$./hello_world

This command will print a string in the terminal.

Hello World!

And Wala! You have your first code written in C++.

Top comments (0)