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++
// Program to show the use of cout // without using namespace #include <iostream> int main() { std::cout << "GeeksforGeeks"; return 0; } |
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++
// Program to show use of using namespace #include <iostream> using namespace std; int main() { cout << "GeeksforGeeks"; return 0; } |
GeeksforGeeks
What would happen if neither “using namespace std” nor “std::” is used for cout?
C++
// Program without using // using namespace std and std:: #include <iostream> int main() { cout << "GeeksforGeeks"; return 0; } |
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.
Recommended Posts:
- Difference between cout and puts() in C++ with Examples
- Cin-Cout vs Scanf-Printf
- Difference and Similarities between PHP and C
- Difference between Stop and Wait protocol and Sliding Window protocol
- Similarities and Difference between Java and C++
- Difference between Yaacomo and and XAP
- Difference between VoIP and and POTS
- Difference between "int main()" and "int main(void)" in C/C++?
- Difference between ++*p, *p++ and *++p
- Difference Between DOS and Windows
- Difference between User Level thread and Kernel Level thread
- What’s difference between The Internet and The Web ?
- Difference between Priority Inversion and Priority Inheritance
- What’s difference between Linux and Android ?
- What’s difference between header files "stdio.h" and "stdlib.h" ?
- Difference between HTML and HTTP
- Difference between http:// and https://
- What's difference between MMU and MPU?
- What's difference between Microcontroller (µC) and Microprocessor (µP)?
- What's the difference between Scripting and Programming Languages?
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.

