C language also has a namespace can be divided into 4-bit class namespace.
Class 4 namespace:
All label (label) belong to the same namespace. Description: ① in the same function, you can have the same label. ② in the same function, and other variables can label the same name. Because they belong to different namespaces.
struct
,enum
Andunion
the name, called tag in C99, all tag belong to the same namespace. That is, if you have declaredstruct A { int a };
no longer declareunion A{ int a };
a.
The reason for all of the tag form a namespace, as always with the previous tagstruct
,enum
orunion
keywords, so the compiler can distinguish them from other identifiers area.struct
Andunion
members belong to a namespace, and are independent of each other. For example: If you have already declaredstruct A { int a };
, the name of its members isa
, you can still declarestruct B{ int a };
orunion B{ int a };
. The reason forstruct
andunion
the members of their respective become a namespace is because their members to access, or need. "" "->" operator, but not alone, so the compiler they can be with other identifiers differentiate. Since the enumerated typeenum
member can be used alone, it is not a member of enumerated types in this namespace.All other identifiers, belong to the same namespace. Including variable names, function names, function parameters, macros,
typedef
type name,enum
members, and so on. Note: If this is the same name identifier appears, the macro definition will cover all other identifiers, because it deals with the pre-processing stage rather than at compile stage. In addition to the macro definition identifier, other types of processing rule is: the inner scope will hide enclosing scope identifier.
Usually write C programs never thought there namespaces C language this, what use is it? In the "C language interface and implementation" God for this, the extensive use typedef struct T *T;
of this form (which T
is struct T *
) usage, follow certain convention, this simple command using the C language rules can also get some benefits, make the code more sophisticated, better readability.
More technical Share: Virbox technology blog