> For the complete documentation index, see [llms.txt](https://ezcode.gitbook.io/bnb-cpp/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ezcode.gitbook.io/bnb-cpp/basic/namespace.md).

# 이름공간(namespace)

## 이름공간(namespace)

&#x20;특정 영역에 이름을 붙여주기 위한 문법적 요소이다.\
`::` (범위지정연산자)를 이용해 접근합니다.

```cpp
#include <iostream>

namespace space {
  int num = 1;
  void func() {
    std::cout << "space의 num : " << num << std::endl;
  }
}

namespace otherSpace {
  int num = 2;
  void func() {
    std::cout << "otherSpace의 num : " << num << std::endl;
  }
}

int main(void)
{
  space::func();
  otherSpace::func();
  return 0;
}
```

## 이름공간 함수의 선언부와 정의를 구분하기 <a href="#ec-9d-b4-eb-a6-84-ea-b3-b5-ea-b0-84-ed-95-a8-ec-88-98-ec-9d-98-ec-84-a0-ec-96-b8-eb-b6-80-ec-99-80-e" id="ec-9d-b4-eb-a6-84-ea-b3-b5-ea-b0-84-ed-95-a8-ec-88-98-ec-9d-98-ec-84-a0-ec-96-b8-eb-b6-80-ec-99-80-e"></a>

```cpp
#include <iostream>

namespace space {
  int num = 1;
  void func();
}

namespace otherSpace {
  int num = 2;
  void func();
}

int main(void)
{
  space::func();
  otherSpace::func();
  return 0;
}

void space::func() {
  std::cout << "space의 num : " << num << std::endl;
}

void otherSpace::func() {
  std::cout << "otherSpace의 num : " << num << std::endl;
}
```

## 이름공간의 중첩 <a href="#ec-9d-b4-eb-a6-84-ea-b3-b5-ea-b0-84-ec-9d-98-ec-a4-91-ec-b2-a9" id="ec-9d-b4-eb-a6-84-ea-b3-b5-ea-b0-84-ec-9d-98-ec-a4-91-ec-b2-a9"></a>

```cpp
#include <iostream>

namespace space {
  void func();
  
  namespace otherSpace {
    void func();
  }
}

int main(void)
{
  space::func();
  space::otherSpace::func();
  return 0;
}

void space::func() {
  std::cout << "space의 func 호출" << std::endl;
}

void space::otherSpace::func() {
  std::cout << "space::otherSpace의 func호출" << std::endl;
}
```

## using 명령을 사용하여 이름공간 명시 <a href="#using-eb-aa-85-eb-a0-b9-ec-9d-84-ec-82-ac-ec-9a-a9-ed-95-98-ec-97-ac-ec-9d-b4-eb-a6-84-ea-b3-b5-ea-b" id="using-eb-aa-85-eb-a0-b9-ec-9d-84-ec-82-ac-ec-9a-a9-ed-95-98-ec-97-ac-ec-9d-b4-eb-a6-84-ea-b3-b5-ea-b"></a>

&#x20;`std`는 C++ 표준라이브러리의 이름공간입니다.\
지금까지 `std::cout`, `std::cin`, `std:endl` 처럼 객체에 접근할때 `::`를 써서 접근하였는데 std처럼 자주 쓰이는 것은 `using` 명령을 통행 **기본 이름공간으로 지정**할수 있습니다.

```cpp
#include <iostream>
using namespace std; // std 이름공간을 기본 이름공간으로 지정

int main(void)
{
  char name[100];
  cout << "이름을 입력하세요 : ";
  cin >> name;
  cout << name << "님 안녕하세요." << endl;
  return 0;
}
```

## ::(범위지정연산자)의 또다른 기능 <a href="#eb-b2-94-ec-9c-84-ec-a7-80-ec-a0-95-ec-97-b0-ec-82-b0-ec-9e-90-ec-9d-98-eb-98-90-eb-8b-a4-eb-a5-b8-e" id="eb-b2-94-ec-9c-84-ec-a7-80-ec-a0-95-ec-97-b0-ec-82-b0-ec-9e-90-ec-9d-98-eb-98-90-eb-8b-a4-eb-a5-b8-e"></a>

범위지정 연산자를 이용하여 전역변수에 접근할 수 있습니다.

```
#include <iostream>
using namespace std;

int val = 100; // 전역변수

int main(void)
{
  int val = 20;
  cout << val << endl; // 지역변수 출력
  cout << ::val << endl; // 전역변수 출력
  return 0;
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ezcode.gitbook.io/bnb-cpp/basic/namespace.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
