← Back to C++ Course | Chapter 6: Arrays & Strings | Lesson 13 of 15

C++ using namespace std

using namespace std brings every name from the std namespace, including string and cout, into scope unqualified, trading a small risk of naming conflicts for shorter, more readable code.
Syntax
cpp
using namespace std;  // then write cout instead of std::cout

std::name;  // without the directive

What is the std Namespace?

The standard library's classes and functions, like string, cout, and vector, all live inside a namespace called std, which groups them together and avoids clashing with names defined elsewhere.

Example: What is the std Namespace?

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Print to the console with cout
	std::cout << "cout lives inside the std namespace" << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

The using namespace std Directive

Writing using namespace std once near the top of a file brings every name from std into the current scope, letting cout, string, and others be written without the std:: prefix afterward.

Example: The using namespace std Directive

cpp
// Include std::cout and std::cin
#include <iostream>
// Lets us write cout instead of std::cout
using namespace std;

// Program execution starts in main()
int main() {
	// Print to the console with cout
	cout << "No std:: prefix needed now" << endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Avoiding the Prefix for Strings Specifically

Rather than importing the entire std namespace, using std::string alone brings in just the string name, which is a more targeted way to shorten code without pulling in every other std name.

Example: Avoiding the Prefix for Strings Specifically

cpp
// Include std::cout and std::cin
#include <iostream>
// Include std::string
#include <string>
using std::string;

// Program execution starts in main()
int main() {
	string name = "Alex";
	// Print to the console with cout
	std::cout << name << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Why Avoid using namespace std in Headers

Placing using namespace std inside a header file forces that namespace import onto every file that includes the header, which can cause unexpected naming conflicts far from where the directive was written.

Example: Why Avoid using namespace std in Headers

cpp
#include <iostream>

// A header should never contain "using namespace std;" -- every file that
// includes it would be forced to import the whole namespace too.
int main() {
	std::cout << "Prefer std:: explicitly in headers" << std::endl;
	return 0;
}

Naming Conflicts Without a Prefix

Importing the entire std namespace risks a naming conflict if the program defines its own identifier with the same name as something in std, since the compiler can no longer tell which one is meant.

Example: Naming Conflicts Without a Prefix

cpp
#include <iostream>
using namespace std;

int count = 5; // fine here, but risks colliding with any "count" from std

int main() {
	cout << count << endl;
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Putting using namespace std; in a header file, which pollutes every file that includes it.
  2. Using using namespace std; and defining your own count, which conflicts with std::count.
  3. Forgetting std:: in a header, so names are undefined.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.