Difference between cout and std::cout in C++

The cout is a predefined object of ostream class, and it is used to print the data on the standard output device. Generally, when we write a program in Linux operating system for G++ compiler, it needs “std” namespace in the program.We use it by writing using namespace std; then we can access any of the objects like cout, cin.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// Program to show the use of cout
// without using namespace
  
#include <iostream>
int main()
{
    std::cout << "GeeksforGeeks";
    return 0;
}

chevron_right


Output:

GeeksforGeeks

std:cout: A namespace is a declarative region inside which something is defined. So, in that case, cout is defined in the std namespace. Thus, std::cout states that is cout defined in the std namespace otherwise to use the definition of cout which is defined in std namespace. So, that std::cout is used to the definition of cout from std namespace.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// Program to show use of using namespace
  
#include <iostream>
using namespace std;
int main()
{
    cout << "GeeksforGeeks";
    return 0;
}

chevron_right


Output:

GeeksforGeeks

What would happen if neither “using namespace std” nor “std::” is used for cout?



C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// Program without using
// using namespace std and std::
  
#include <iostream>
  
int main()
{
    cout << "GeeksforGeeks";
    return 0;
}

chevron_right


Compilation Error:

main.cpp: In function ‘int main()’:
main.cpp:5:2: error:
  ‘cout’ was not declared in this scope
  cout<<"GeeksforGeeks"<<endl;
  
main.cpp:5:2: note: suggested alternative:
In file included from main.cpp:1:0:
/usr/include/c++/7/iostream:61:18: note:   ‘std::cout’
   extern ostream cout;  /// Linked to standard output

Difference between “using namespace std cout” and “std::cout”?

In C++, cout and std::cout both are same, but there are some basic differences are following:

S. No. cout std::cout
1. namespace std must be used in the program Without using namespace std, you should use std::cout.
2. cout is a predefine object of ostream class it is used to print the data as well as values
3. Using namespace std; then we can access any of the object like cout, cin  without using std, but if we do not use using namespace std; then we should use std::cout etc to prevent errors

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.