codegurukool.com · Free coding tutorials & online compiler

Home › C++ Tutorial › C++ namespaces

C++ namespaces

Avoid name clashes.

A namespace groups names. Use name::item. Avoid using namespace std; in large projects.

Example code

#include <iostream>
namespace Math { int twice(int x) { return x * 2; } }
int main() {
  std::cout << Math::twice(21) << std::endl;
  return 0;
}

Run this code in the online editor

← C++ algorithmsC++ how to: add two numbers →