using namespace std in C++

By | September 11, 2017
Spread the love

using namespace std in C++ | IMPORTANT POINTS IN C++

Using namespace std in C++

Let’s consider two programs to study the usage of using namespace std in C++,

// my first program in C++
#include <iostream>
int main()
{

std::cout << "Hello World!";

}
Hello World!
// my second program in C++
#include <iostream>
using namespace std;
int main ()
{
  cout << "Hello World! ";
}
Hello World!

 1) Let’s examine the first program,

std::cout << "Hello World!";

It represents, “Hello world” inserted into the standard output.

What follows next to << will be put into standard output.

*String should be put in the inverted commas.

*Each statement in C++ program should terminate with a semi-colon (;).

*Forgetting semicolon is the most common cause for syntax error.

2) Now second program,

using namespace std;

using declaration( using namespace std;)

*It represents that Cout belongs to namespace standard output.

*Contains all the classes and object functions of the standard C++ library.

<<– insertion stream operator.

*It transfers the content present in its right to the object in left.

Right operand—“Hello world!”

Left operand—Cout object

Conclusion from two programs,

*Both program will give the same output.

*All standard library functions are present in– using namespace std –so there is no need to write again std:: in the program statements.

* Both ways are valid in C++, produce same exact behavior.

Some essential points you should remember in C++,

*All C++ statement should end with the semi-colon.

*There is no rule in C++ to write the code one by one,

Example::

// my second program in C++
#include <iostream>
using namespace std;
int main ()
{
  cout << "Hello World! ";
}
Hello World!
// my second program in C++
#include <iostream>
using namespace std;
int main ()
{
  cout << "Hello World! ";
}
Hello World!

Both programs give the same output.

*Separation of the line is only for our readability, there is no strict rule to separate line.

// This program just displays a string and exits, variation 1
#include <iostream>
  
int main() { std::cout<<"Hello World!"; std::cout<<std::endl; return 0; }
Hello world!
// This program just displays a string and exits, variation 2
#include <iostream>
 int main() {
  std::cout << "Hello World!";
  std::cout << std::endl;
return 0;
}
Hello world!

YOU MIGHT LIKE THIS

HYPERLOOP-THE FIFTH MODE OF TRANSPORTATION

WHY KOTLIN IS THE EASIEST ANDROID DEVELOPMENT LANGUAGE?

HOLOLENS-YOU CAN CREATE YOUR DESIRED WORLD

WHY IS SOCIAL MEDIA MARKETING  IN BUZZ?

HOW INTERNET WORKS? WHO OWNS IT?


Spread the love
Category: C++ Tutorials Tags:

About Manojkumar M M

Manojkumar is a Digital Marketing Expert from Bangalore, India. He has got 3 years of experience in the field of Digital Marketing. He worked with many start-ups to help them in improving their Business Lead Generation through online. He blogs about digital marketing tips, technology updates, programming language tips at this blog Techgeetam.com. Contact me for any kind of help in the field of Digital Marketing. Mail Id - digimanoj98@gmail.com

Leave a Reply

Your email address will not be published.