Thursday, March 26, 2009

Difference between cout and std::cout?

When you use std:: before cout you are telling the compiler that you want to use the name cout that is in the namespace called standard. There are lots of ways you can do it.

1)

CODE

#include <iostream>
int main()
{
using std::cout; using std::endl;
    cout << "Hello, world" << endl;
return 0;
}

2)

CODE

#include <iostream>
using namespace std;
int main()
{
    cout << "Hello, world" << endl;
return 0;
}

3)

CODE

#include <iostream>
int main()
{
    std::cout << "Hello, world" << std::endl;
return 0;
}

It is just a matter of personal style. I personally use number 1.
But using it this ay you have to remember all the names that you wish to use and say that you want to use them. Only the names in that scope can use them.

No comments:

Post a Comment