SEARCH THIS BLOG

WHY IS "using namespace std" USED IN C++?

You must have noticed, all the programs in C++ start with the following two lines of code:

      #include<iostream>

      using namespace std;

Well, let us discuss about these lines of codes in detail.

 

What is # ?

The ‘#’ in C++ is called ‘Pre-processor directive’. The statements starting with ‘#’ are executed before the compilation of program starts, so that the compiler knows all the header files whose members we might have used in our program.

 

What is iostream?

 

All pre-defined functions in C++ are defined under certain files which are called the header files. These in-built functions include functions such as cout, cin, strlen, strcmp, abs, sqrt, pow, sort, etc.

The inbuilt functions cout, cin, endl, etc are defined inside iostream header file. So now we come to the answer :

iostream is a header file in C++ that contains all the predefined functions controlling the input and output operations.

 

What is #include<iostream>?

 

Whenever we write a program that needs to take input or print something as output, we need cin and cout statements. These are defined in iostream. So, we need to copy the iostream header into our program. The statement #include<iostream> does exactly this thing. It copies all the content of iostream header into our program so that we can use all the pre-defined functions and statements it contains.

 

What is a namespace?

 

In programming we cannot have variables, functions etc of same name. So, it becomes tiring when we need to name hundreds of new variables. This problem can be solved using a namespace. Namespaces allow us to use same name variables by dividing them into different groups.

Let us understand using a daily life example.

Suppose your class in school has 4 sections, A, B, C & D. There are 50 students in each of these sections. Now there is a high probability that out of the total 200 students, few students have the same name. Now if two students with same name are present in one section, it becomes tough to mark them. But if they are in two different sections, this problem is solved as they can be properly identified using their section name. Here the sections are playing the role of namespaces.

We cannot use two variables with the same name in a program. So, we put them in two different namespaces and then we reference them easily through their respective namespace.

The following code will make this concept clearer.


 Output screen:   5

                              10


What is std?

 

std is just a namespace in the header file iostream. This namespace contains all the members of iostream like cin, cout, endl etc. In order to use these statements, we need to tell the compiler that we are using namespace std.

If we ignore this statement, we can still access cin, cout statements as std::cin, std::cout, etc.

If we do not tell the compiler which namespace we are using, it will generate error because it will have no idea about the cin cout statements.

 

Let us sum up all that we have learnt.

To take input or to provide output, we need the input output streams and functions which are defined in the std namespace of the iostream header file. By the statement #include<iostream>, we tell the compiler to copy all the information from iostream header file and paste it in our program. The using namespace std statement tells the compiler that we wish to access all the functions and statements defined in the std namespace of the iostream header file in our program.

After these two statements, the compiler knows exactly which statements we want to access and thus allows us to use cin, cout, endl etc without showing any error.




If you find something wrong or if you wish to add some information in this article, feel free to comment on this post or contact us at coding.nkcoder@gmail.com.

Which topics you want an article on? Comment down on this post.

Do not stop here. Read all articles related to C++ here.

If you want to contribute any article for the website, feel free to share it on coding.nkcoder@gmail.com. We will publish it on the website with your name.


Post a Comment