why do we need to write using namespace std; in every program.
Using namespace std
@Gauravlakhina
‘std’ stands for ‘standard’ . using namespace std ; tells the compiler to use the standard namespace whenever it can.
Basically there are several variables that are defined in some libraries having same name. Say you make a varable named ‘count’ . Now it is very likely that another variable having same name as ‘count’ is defined in some other library as well which you are including in your program. The compiler gets confused as to which ‘count’ to refer if any operation is called on it.
A namespace is used to differentiate between variables having same name.
cin and cout are defined in multiple libraries having different functionalities and we don’t really use these other versions unless you are going for advanced development. Nonetheless , they are defined . So to tell the compiler we wish to use the standard cin and cout , we have to use them as
std :: cout and std :: cin .
Consider this program
#include
int main() {
int n ;
std :: cin >> n ;
std :: cout << " n = " << std :: endl ;
return 0;
}
This is a valid program. But to prevent writing std again and again everytime , we tell the compiler to use std whenever it can in the beginning only using the ‘using namespace std’ statement .
#include
using namespace std;
int main() {
int n ;
cin >> n ;
cout << " n = " << endl ;
return 0;
}
This is the same program but simpler.