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.
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.
What would happen if neither “using namespace std” nor “std::” is used for cout?
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