You may not know, define, declare, initialize, declare Initialization

Source: Internet
Author: User

You may not know, define, declare, initialize, declare Initialization

Declaration definitions are everywhere, but they are not as simple as they are.
First, from a broad perspective, a declaration contains a definition, which can also be referred to as a "declarative definition". In a narrow sense, a declaration can be called a "reference Declaration ".
Next, we will discuss "declarative definition" and "reference Declaration ". Definition and declaration.

1. The system allocates space for the Defined variables during definition, but does not allocate space for declaration. This is essentially a difference.

2. Definitions can only appear once in a program, but can be declared multiple times. For example:
If two c instances have the same global variable "Definition" and are not initialized, the compiler considers the second variable as declaration rather than definition.
Therefore, writing two int a; compilation will not go wrong, but writing int a = 1; int a = 1; Compilation error. (Same for functions)

3. Visible from 2, the side effect of initialization during variable definition is to tell the compiler that this is a definition rather than a declaration. This is because it cannot be declared.
The specific example is extern int a. This indicates that this is a declaration rather than a definition. If it is written as extern int a = 1, an error is reported during compilation.
In combination with 1, this is normal, because the Declaration does not allocate space. How to assign values?

4. Declaration can be completed during definition and initialization! For example, int a = 0; // definition, declaration, and initialization contain this statement.
// -------- Usage of extern -------------------
Extern is used to declare and call global variables or functions of other files.
Since the Declaration can be performed multiple times, some variables will be called by other file files, so the change volume -- extern + variable will be declared more once in the header file of the file.
If another file contains this header file, you can call this variable.
// -------- Initialize the Declaration definition of struct -------------------
1. struct definition (Here we note that struct defines the type, not the variable; but the above conclusions also apply !)
Struct people // define the type
{
Char name [20];
Int age;
};

2. struct people; // declaration type. It is not wrong to write two identical statements! However, the type can be defined only once.

3. When defining a type, use the type declaration (Definition) variable. The class must have been defined, and if s1 does not have any other initialization code, the Declaration is defined here.
Struct student
{
Char name [20];
Int age;
} S1;

4. Initialization of struct variables. Under the premise of 3, initialization in this form can be called "out-of-order initialization" without following the defined sequence"
Struct student s1 =
{
. Age = 10,
. Name = "linux", // note that the initialization struct variable uses a comma, while the struct type is defined using a semicolon.
};

5. Omit the type declaration (Definition)
Struct
{
Char name [20];
Int age;
} S1;
But it cannot be initialized as in 4.

6. the struct variable cannot be initialized when the struct type is defined. The following write method will report an error:
Struct student // incorrect description
{
Char name [20] = linux;
Int age = 10;
} S1;

7. Yu seems to have discovered another magical way of writing: This is also effective.
Struct student
{
Char name [20];
Int age;
} S1 =
{
. Age = 10,
. Name = "linux ",
};

8. Remove the type name, which is also valid.
Struct
{
Char name [20];
Int age;
} S1 =
{
. Age = 10,
. Name = "linux ",
};
Define struct types, define struct variables, and initialize variables ~~

9. Differences between struct {} a and struct {}
The former defines the (struct) variable, and the latter defines the struct type.

 

// -------- Initialization of the enum Declaration definition -------------------
In this case, you can directly extract the notes from instructor Zhu youpeng.
/*
**************************************** ************************
* Enumeration type definition
**************************************** ************************
*/
// Define method 1, define type and define Variable Separation
Enum week
{
SUN, // SUN = 0
MON, // MON = 1;
TUE,
WEN,
THU,
FRI,
SAT,
};

Enum week today;

// Define method 2. Define both types and variables
Enum week
{
SUN, // SUN = 0
MON, // MON = 1;
TUE,
WEN,
THU,
FRI,
SAT,
} Today, yesterday;

// Define method 3. Define both types and variables
Enum
{
SUN, // SUN = 0
MON, // MON = 1;
TUE,
WEN,
THU,
FRI,
SAT,
} Today, yesterday;

// Define Method 4. Use typedef to define the alias of the enumeration type and use the alias later to define the variable
Typedef enum week
{
SUN, // SUN = 0
MON, // MON = 1;
TUE,
WEN,
THU,
FRI,
SAT,
} Week;


// Define Method 5. Use typedef to define the alias of the enumeration type and use the alias later to define the variable
Typedef enum
{
SUN, // SUN = 0
MON, // MON = 1;
TUE,
WEN,
THU,
FRI,
SAT,
} Week;

Struct, enumeration, usage summary:
1. There are many similarities between struct and enumeration;
2. When defining the enumeration type, use a semicolon. You can also initialize the actual variables at the same time.
3. The enumerated type, whether it is the type name or the variables in the enumeration are global, so the names cannot be duplicated. The internal variables of the struct are local.
4. Using enumeration, you can directly use the variables such as SUN, and the struct needs to be accessed as follows: s1.age

Finally, the static statement:
Static to declare a variable has two functions:
(1) If you use static Declaration for a local variable, it is the property of a global variable for the variable-always exists throughout the execution period of the program. Because it is in the same data segment as the global variable. Instead of stack.
At the same time, it has the property of local variables-which cannot be accessed by external programs. This is the issue of life cycle.
(2) If a global variable or function is declared as static, the variable is "hidden". The scope of the variable is limited to this file. Other files cannot be accessed.
Not with extern.

Related Article

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.