Using the std namespace and library

Why do we need both a "header file" and a using namespace tag for the normal operation of any library function. For example, cout will not work if we do not use iostream . Also, this will not work unless we use "using namespace std". My question is, why do we need a combination of both using namespace std and #include <iostream> for cout to succeed?

+6
source share
3 answers

Including the library header makes the library function visible to your program code. Without this, your program does not know that the library exists. This is the part that is needed.

Writing using namespace std just lets you write cout , not the full name std::cout . It is convenience that is all.

+6
source

cout defined in the std namespace and you can use it without adding using namespace as

 std::cout << "Hello, World" << std::endl; 
+4
source

Thanks for the answer. But my question is: why do we need this in the first place. Since we exposed iostream, why can't we just use cout.

Why use std :: cout or use the std namespace?

If you are not using the std namespace, the compiler will try to call cout or cin, as if it were not defined in the namespace. Since it does not exist there, the compiler tries to name something that does not exist! Therefore, an error occurs.

+3
source

Source: https://habr.com/ru/post/979987/


All Articles