I know how namespaces work in C++, but I´m a little bit confused of how they work in C. So, I did a bit of research about name spaces in C.
First, the respective section in ISO/IEC 9899:2018 (C18), section 6.2.3:
6.2.3 Name spaces of identifiers
1 If more than one declaration of a particular identifier is visible at any point in a translation unit, the syntactic context disambiguates uses that refer to different entities. Thus, there are separate name spaces for various categories of identifiers, as follows:
— label names (disambiguated by the syntax of the label declaration and use); — the tags of structures, unions, and enumerations (disambiguated by following any(32)) of the keywords struct, union, or enum);
— the members of structures or unions; each structure or union has a separate name space for its members(disambiguated by the type of the expression used to access the member via the . or -> operator);
— all other identifiers, called ordinary identifiers (declared in ordinary declarators or as enumeration constants).
32) There is only one name space for tags even though three are possible.
So this gives me a bit more understanding of the term in C and seems to generally have the same kind of purpose as in C++. But unfortunately, there is nothing further said in the standard about how name spaces work in C.
Apparently, it has something to do with the distinction between entities that share the same identifier and, as opposed to C++, where we declaring namespaces like:
namespace ctrl1
{
int max = 245;
}
and using namespaces, like:
using namespace ctrl1;
or
int a = ctrl1::max;
in C, the compiler is be able to disambiguate a certain use of one object automatically if the respective identifier is used. Correct me, if I´m wrong.
How does that work? How does the compiler know if he shall use one entity instead of the other in C?
I have read Name spaces in c++ and c but the question is more focused on C++ and focused on the handling of a specific example.
I also read the Name spaces in C where again the purpose of the question is more focused on a specific example, here the enum
type.
My Question is:
- How do name spaces work in C?
strut.val
orstrut->val
can’t be anything else than a member of that specific struct. The language syntax is unambiguous about these. – Sami Kuhmonen