CodeBlanks Logo Icon CodeBlanks

std namespace

Learn the std namespace and its common usage.

The std namespace

The C++ standard library lives under the std namespace. Frequently used tools like cout, cin, string, vector are all in this namespace.

std::cout << "Hello";
std::string s = "x";

Here you cannot access these tools without the std:: prefix. In small programs, it is common to write using namespace std; to drop the prefix; in large projects, the full name std::cout is preferred.

using namespace std

In small programs, using namespace std; is used to avoid writing std:: every time. After this line, names like cout, cin, string can be used directly.

using namespace std;
cout << "x";  // no std:: needed
string s = "y";

This is a common convenience in teaching examples and small tools. But it is not recommended in header files or large projects due to the risk of name clashes; in those cases the std:: prefix is used.

Practice Print with std::cout

This time, do not use using namespace std. In main, print with std::cout << "Hello" << std::endl;. Use the std:: prefix for both names.

Starter code

main.cpp
#include <iostream>

int main() {
    // print output with the full name
    return 0;
}

Sample solution

main.cpp
#include <iostream>

int main() {
    std::cout << "Hello" << std::endl;
    return 0;
}

Expected output

Hello

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Fill in the blank: std access

Question
std____cout << "Hello";
Answer
std::cout << "Hello";

Explanation: The :: operator reaches cout inside std.

2 Match each expression with its source

Term Meaning
std::cout output stream
std::string string type
std::vector dynamic array

3 What is the full form of std::cout?

  • std::cout Correct answer
  • cout
  • iostream::cout
  • std.cout

Explanation: std::cout is the full name; the prefix-less form works only after using.

All lessons in Beginner