C language Namespaces

Source: Internet
Author: User

In a programming language, a namespace is a special scope , and itself is represented by an identifier, which organizes a series of logically related identifiers together with an identifier.

The namespace is to resolve how to differentiate between identical identifiers within the same scope.

Description

(1) namespaces can be used to differentiate identifiers only in the same scope, and the concept of namespaces is not used in nested scopes and different scopes to differentiate identifiers.

(2) Within the same scope,

Identifiers must be different if namespaces are the same;

Identifiers can be the same if the namespaces are different.

Second, according to C99, the name space is divided into four kinds:

(1) All labels belong to the same namespace.

(2) The names of structs, unions, and enums are called tags in C99, and all tags belong to the same namespace.

In other words, if you have declared struct a {int a}; You can no longer declare union a {int A};

Description: The reason why all tags make up a namespace is that the compiler can separate them from the other identifiers because the struct, union, and Enum keywords are always in front of tag.

(3) The members of the struct and Union are in their respective struct or union namespace, independent of each other, and can form a recursive namespace (such as a struct in defining a struct).

For example, if you have declared struct a {int a}; Its member name is a, you can still declare struct B {int a}; or union B {int A};

Description: The reason that struct and union members each become a namespace is because their members are accessed through the "." Or operator, which is not used alone, so the compiler can separate them from the other identifiers. Because members of enum-type enum can be used alone, members of an enumeration type are not in this namespace class.

(4) All other identifiers that belong to the same namespace. Includes variable names, functions, function parameters, macro definitions, typedef type names, enum members, and so on.

Note: If the identifier has a duplicate name, the macro definition overrides all other identifiers because it is processed during the preprocessing phase rather than the compile phase. In addition to other categories of identifiers other than macro definitions, the processing rule is that the inner scope hides the identifier of the outer scope.

#include <stdio.h>
#include <stdlib.h>
int main ()
{
struct A
{
int A;
};

Union B
{
int A;
};

struct a A;
Union b b;
int my_label = 1;
A.A = 1;
B.A = 20;
printf ("B.A = =%d \ n", B.A);

My_label:
printf ("a.a = =%d \ n", A.A);
A.A + = 1;
if (A.A <= 5)
{
Goto My_label;
}

System ("pause");
return exit_success;
}

Example 2:

#include <stdio.h>
typedef struct A {
int A;
struct A *a; Tag and struct members
A
void Main ()
{
A;
int B = 3;
A->a = (struct) &B;
printf ("%d\n", * (A->a));
//

int B = 3;
A.A = &B;
printf ("%d\n", * (A.A));

A.A = 3;
printf ("%d\n", A.A);

}

C language Namespaces

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.