what is namespace in c

11 months ago 43
Nature

In C++, a namespace is a declarative region that provides a scope to the identifiers (the names of types, functions, variables, etc) inside it. It is used to organize code into logical scopes referred to by names, allowing the elements of programs to be grouped into different scopes. Namespaces provide the space where we can define or declare identifiers such as names of variables, methods, classes, etc.

A namespace definition begins with the keyword "namespace" followed by the namespace name. Multiple namespace blocks with the same name are allowed, and all declarations within those blocks are declared in the named scope. A namespace can be declared in multiple blocks in a single file, and in multiple files. The compiler joins the parts together during preprocessing, and the resulting namespace contains all the members declared in all the parts.

Namespaces may be nested, and an ordinary nested namespace has unqualified access to its parents members, but the parent members do not have unqualified access to the nested namespace (unless it is declared as inline) .

In summary, a namespace is a feature in C++ that allows us to group named entities that otherwise would have global scope into narrower scopes, giving them namespace scope.