C++ Hello World Application – POFTUT

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.

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

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  How To Download, Install Android Studio On Windows?

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.

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.
LEARN MORE  Commonly Asked C Programming Interview Questions and Answers

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.

Compile Hello World Program Into Executable and Run

Compile Hello World Program Into Executable and Run

Leave a Reply

Your email address will not be published. Required fields are marked *