Reputation: 8102
Why we need both the "header file" and the using namespace
tag for the any library function to get executed properly. For example cout
will not work unless we use iostream
. Also it will not work unless we use "using namespace std". My question is why do we need combination of both using namespace std
as well as #include <iostream>
for cout
to execute successfully?
Upvotes: 6
Views: 2763
Reputation: 4131
Thanks for the reply. But my question is why do we need it in the first place.
Since once we exposed "iostream" then why can't we simply use cout.Why to use std::cout or using namespace std?
When you don't use the std namespace, compiler will try to call cout or cin as if it weren't defined in a namespace. Since it doesn't exist there, compiler tries to call something that doesn't exist! Hence, an error occurs.
Upvotes: 3
Reputation: 385264
Including a library header makes the library feature visible to your program code. Without that, your program has no idea that the library even exists. This is the part that is necessary.
Writing using namespace std
simply allows you to write cout
rather than the full name which is std::cout
. It's a convenience, that's all.
Upvotes: 7
Reputation: 201467
cout
is defined in the std
namespace, and you can use it without adding a using namespace
like
std::cout << "Hello, World" << std::endl;
Upvotes: 4