C++ Hello World Application


Hello World is a popular term used in programming courses. Hello World means a new start for a programming language or applications. It is used as a salute from the first application to its developer and the world like a newborn baby.

Hello World History

Before starting to define and create a hello world we need to learn the history start of the hello world. The c programming language is created in order to develop a popular operating system named Unix. Brian Kernighan the creator of the C published a reference book named C Programming Language in order to describe and reference the C programming language in 1973. The introduction and first example was a simple C program that simply outputs the text “Hello World” to the standard output or screen. The example hello world code was like below.

main( ) {
extrn a, b, c;
putchar(a); putchar(b); putchar(c); putchar(’!*n’);
} 1 ’hell’;
b ’o, w’;
c ’orld’;

This clever introduction example and text is used by other authors and programming languages later which make the “Hello World” so much popular. “Hello World” is repeated over and over again by new programming languages even in 2020 after 47 years later.

Simple Hello World Program Source Code

Below we can see a simple HelloWorld applications source code. In general, these applications will print “Hello World” to the standard output which is generally the console or command-line interface.

Simple Hello World Program Source Code
Simple Hello World Program Source Code
//Include the input output library iostream
#include <iostream>


//Create namespace named std
using namespace std;


//Application enterance function main()
int main()
{

    //Print "Hello World" to the standard output
    cout << "Hello, World!\n";

    //Main function return value
    return 0;

}

Let’s explain the given code step by step.

  • // lines are comments which are not executed. They are just comments and explanations.
  • #include <iostream>  is used to import and include input and output libraries which provides cout and cin. #include is a C++ directive that will import or include a given library. <iostream> is a library that provides functions to read and write from standard input and output.
  • using namespace std; is used to create and set namespace. A namespace is used to create a block of code that will be effective in the current source code page.
  • int main() is a method definition but a special one. main() function is a special name where it is used to create a starting point for the application or executable file. { and } are used to specify the start and end of the main function block. All codes related to the main function will be stored inside these curly braces.
  • The most magical part is cout << "Hello World!\n;" which will print “Hello World”  to the standard output and \n will be used to set the end of the line and put cursor to the next line.
  • return 0; will return the main function with value 0. Actually, this is a standard function convention that generally has no meaning with the special main function.
LEARN MORE  Things To Do With Computer When Bored

Hello World Example with Input and Output

The hello world example can be extended with some input from the user. We will use the cin keyword which will read data from the standard user input and out the data to the given variable.

//Include the input output library iostream
#include <iostream>


//Create namespace named std
using namespace std;


//Application enterance function main()
int main()
{

    //Print "Hello World" to the standard output
    cout << "Hello, World!\n";

    //Create a string variable name
    string name;
    //Read from standard input and put data into name variable
    cin >> name;
    //Print "Hello"  with the name variable
    cout << "Hello " << name <<"\n";

    //Main function return value
    return 0;

}
Hello World Example with Input and Output
Hello World Example with Input and Output

We will just talk about the differences from the previous example code.

  • string name; is used to create a variable as named name. We will store the user to input into the variable name.
  • cin >> name; will read from the standard input which is generally command-line interface and put the data into the variable named name.
  • cout << "Hello" <<name << "\n"; will print the Hello to the standard output with the name variable data. "\n" is used for the end of the line which will put cursor to the next line.

Compile Hello World Program Into Executable and Run

Just creating the source code will not create an application or executable file. We have to compile the given source code. There are different ways like using IDE or command-line tools. For the Linux system, we will use the g++ compiler. We will also provide the -o HelloWorld option to set created executable name and the source code file HelloWorld.cpp to the g++ compiler. The cpp extension is used for C++ source files. It is not mandatory but useful for others to understand the file type.

$ g++ -o HelloWorld HelloWorld.cpp

$ file  HelloWorld

$ ls -lh HelloWorld

$ ./HelloWorld
Compile Hello World Program Into Executable and Run
Compile Hello World Program Into Executable and Run

Leave a Comment