Namespace C language

C language also has a namespace can be divided into 4-bit class namespace.

Class 4 namespace:

  1. 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.

  2. struct, enumAnd unionthe name, called tag in C99, all tag belong to the same namespace. That is, if you have declared struct A { int a }; no longer declare union A{ int a };a.
    The reason for all of the tag form a namespace, as always with the previous tag struct, enumor unionkeywords, so the compiler can distinguish them from other identifiers area.

  3. structAnd unionmembers belong to a namespace, and are independent of each other. For example: If you have already declared struct A { int a }; , the name of its members is a, you can still declare  struct B{ int a };or union B{ int a };. The reason for structand unionthe 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 type enummember can be used alone, it is not a member of enumerated types in this namespace.

  4. All other identifiers, belong to the same namespace. Including variable names, function names, function parameters, macros, typedeftype name, enummembers, 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 Tis 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


Guess you like

Origin blog.51cto.com/senseshield/2415666