cout HELP !!

- is there any difference between cout and std::cout ?

- if yes then please explain it.
cout is just the same as std::cout but you need:
using namespace std;
In your code just below the includes if you want.
They are the same. cout is in the std:: namespace. The definition is something like this:

1
2
3
4
namespace std
{
    ostream cout;
}


To access an object in the std namespace, you can prepend std:: to the variable, or you can use the using keyword like: using namespace std; or using std::cout;.

I prefer prepending std:: to my variables when they aren't used often. If I use them all of the time, then I like using std::cout;. I don't use: using namespace std; because std is huge and it brings in many functions that I don't need. It causes the potential for naming conflicts.

For example, if I make a function called sort, when I add using namespace std; I have a problem in that it might try and call std::sort instead of my own version.

See this for more info:
http://www.cplusplus.com/doc/tutorial/namespaces/
Last edited on
is there any difference between cout and std::cout ?

The former is an unqualified name and the latter is a qualified name.
thanks stewbond
a language purist would say you shouldn't employ either version of 'using' and that you should always prefix each and every element from a namespace with its identifier. In my opinion, that's like calling your best friend by hist first and last name all the time. It just seems a little too formal.

if you hate typing, you can employ the 'using' directive. A decent compromise is to employ 'using' declarations.
Last edited on
The really hardcore purist would write ::std::cout in case someone creates their own namespace called "std", nested within another user-defined namespace, and sneaks in a using directive
Topic archived. No new replies allowed.