If you are already familiar with C++ programming then you must have seen this line "using namespace std" on top of the code after including all required header files. Many beginners consider this as a part of C++ syntax without trying to understand its exact meaning. In this article, we will discuss namespace and why we use it.
What is Namespace in C++?
A namespace is a declarative region that provides a scope to the identity functions or variables inside it. They are used to organize code into logic groups and prevent name collisions, mainly when the code base includes multiple libraries.
Let's understand it using one example condition:
#include<iostream> int main() { int n = 0; std::cout<<"Enter a number: "; std::cin>>n; std::cout<<"The entered number is: " << n; return 0; }
Enter a number: 5
The entered number is: 5
- syntax: using namespace::name;
#include<iostream> using std::cin; using std::cout; int main() { int n = 0; cout<<"Enter a number: "; cin>>n; cout<<"The entered number is: " << n; return 0; }
Enter a number: 5
The entered number is: 5
C++ using namespace std Example.
#include<iostream> using namespace std; int main() { int n = 0; cout<<"Enter a number: "; cin>>n; cout<<"The entered number is: " << n; return 0; }
Enter a number: 5
The entered number is: 5
No comments:
Post a Comment