Namespace Std Program Example using CPP

  1. Home
  2. Tutorials
  3. CPP
  4. CPP Programs
  5. Namespace
  6. Program

Source Code:

#include <iostream>

/*
Standard namespace using
namespace directive and
without namespace directive
*/

int main(int argc, char** argv) {
    
    // variable declaration statement
    int x;
    
    // input and output without using namespace directive
    std::cout<<"Enter Number: ";
    std::cin>>x;  // cin without namespace directive
    std::cout<<"You Entered: "<<x<<std::endl;
    
    // using namespace directive
    using namespace std;
    cout<<"Enter Number: "; // cout with namespace directive
    cin>>x;
    cout<<"You Entered: "<<x<<endl;
         
    return 0;
}

Output:

c++ std namespace program example program

Working:

Standard namespace in C++ is used to access standard input and output objects such as cin and cout (console output). In this program example we use standard namespace std witha nd without using namespace directive. When we use namespace directive in program then there is no need to use scope resolution operator with standard objects.

Here we also used endl manipulator that also belong to standard namespace in C++. It is good practice to use namespace directive in C++ programs. This program is compiled and tested using Dev-C++ IDE.

Comments
Login to TRACK of Comments.