First C++ Program – Hello World

We have completed the basic introduction to C++ and the language features. Hoping that you have set the environment necessary to run the C++ programs in your machine. Let us jump to write first C++ program to print hello world. Before that we need to know some basics of the language.

The ISO C++ standard defines two kinds of entities:
1. Core language features, such as built-in types and loops.
2. Standard-library components, such as containers and I/O operations.

The standard-library components are perfectly ordinary C++ code provided by every C++ version implementation. Actually the C++ standard library is implemented in C++ itself which makes us know that the language is expressive enough and powerful. We’ll discuss more about them in detail when we move on to further tutorials.

 

First C++ Program โ€“ Hello World

#include<iostream>	//header file

using namespace std;

int main ()
{
	cout<<"Hello Wolrd";
	return 0;
}

 

Output

First C++ Program - Hello World

 

int main()

Like C, C++ is also a collection of several functions. In the above program we have only one function named main(). This is the necessary default function that every program must contain. Its return type may vary according to the user requirement. Execution of C program begins from the main function. The compiler ignores the white spaces, and carriage returns. You can indent the tokens with any number of white spaces. Just like C, every program statement must end with a semicolon in C++. The whole code comes inside curly braces {}. It shows the body of the main() function

 

Comments

If you are familiar with C, you might remember that it allows us multiple line comments enclosed in /* and */ comment symbols. These are still valid in C++. Comments increases the readability and makes the code easy to understand.

/* this is a multiple line comment
here you can comment about the program
author name, inputs, outputs, control flow etc.
*/

Apart from this multiple line comments, C++ introduced single line comments with a // symbol preceding the comment. You can see the first line of the program, to see such type of comments. They allow only single line comments and these are ignored by the compiler.

// this is a single line comments
// you can give a short description here of the line or about of the following code block

 

#include<iostream>

The first line of the program tells the preprocessor to include contents of iostream header file to the program. We use this header file as it contains the declarations of identifier cout and the operator <<.

ANSI C++ introduced new changes to the header files. Usually we used to write the header files in old versions of C++ as “iostream.h”. If your compiler doesn’t support ANSI C++ then make sure that you change the header file as “iostream.h”. Note that some implementations also use as “iostream.hpp”. We must include the proper header files according to our compiler implementation.

Old version
#include<iostream.h>

New version
#include<iostream>

 

using namespace std

Namespace is a new concept in ISO C++. This defines the scope of identifiers that we use in our program. For using the identifiers defined in namespace scope we need to include it in the program. In above program std is the namespace where the C++ standard libraries are found. As we discussed at the start, we have a standard library and core language features.

 

cout & <<

See the above program, we have written a single statement in the main() function that prints the message to the standard output screen. This statements helps us to know about two new features of C++. First is the object and the second is the operator overloading. cout is a predefined object that represents the standard output in C++. The operator << ย sends the given message in quotes to the standard output screen using the object cout.

You may get a doubt that the operator << is used as bit wise left shift operator in C. Yes it also serves the same purpose in C++. We already are familiar that C++ exhibits polymorphism which means existing in many forms depending on the context. Here we use the operator << as put to operator. And in some cases, we many use it as bit wise left shift operator. This concept is called Operator Overloading.

 

So this was our first C++ program to print hello world. You have seen so many strange terms in above tutorial. Don’t worry about them, we will discuss them in upcoming tutorials. If you have any doubts then feel free to ask it by commenting below.

9 thoughts on “First C++ Program – Hello World”

    1. Actually it is not the part of code, there was some issue in the CSS, I have fixed it now. Thanks for telling ๐Ÿ™‚

  1. Your tutorials are very easy to understand and on point. I’m looking forward to more tutorials on c++. Keep up the good work. ๐Ÿ˜€

    1. I am so happy to know that my tutorials are helpful for you. Very soon I will start posting more C++ tutorials. So keep visiting ๐Ÿ™‚

  2. dear bro actually i dnt have basic programming language like a,b,c for a kid….so can u suggest me wts best way to understand c language completely…bcz without basics this all is just like a puzzle for me…. i will be thankful if u suggest me how can i be a master of c language………..

Leave a Comment

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