[Essential knowledge of C language 2] (detailed explanation of arrays and static)

Table of contents

 

7.Array

7.1 Array definition

7.2 Initialization of array

7.2.1 Complete initial recognition

7.2.2 Incomplete initialization

7.3 Array subscripts

8.static

8.1 Connection properties:

8.2 Classification of connection attributes: 

8.3 Storage types

1. auto variable:

2. register variable:

3. static variables

4. extern external variables

8.4 static modification 

static modified local variables - called static local variables

2. Modify global variables - called static global variables

3. Modified functions - called static functions


7.Array

7.1 Array definition

In C language, if we want to define 10 numbers. what will you do?

#include <stdio.h>
int main()
{
    int a1 = 1;
    int a2 = 2;
    int a3 = 3;
    int a4 = 4;
    //……
    int a10 = 10;
    return 0;
}

Is it like the code above? If we define ten numbers like this, the efficiency is quite low. C language provides us with the concept of arrays. An array is a collection of elements of the same type . Let’s see how to use it:

#include <stdio.h>
int main()
{
	//定义一个整型数组,里面存放10个数字
	int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };
	return 0;
}

Summary: The definition of an array can be understood as: type + array name [ ], where [ ] is the number of elements.

[ ] must be a constant or constant expression;

#inlcude <stdio.h>
int main()
{
    //整型数组
    int arr1[3] = {1, 2, 3};

    //字符型数组(字符串)
    char arr2[3] = "hhh";
    return 0;
}

7.2 Initialization of array

7.2.1 Complete initial recognition

Initializing all elements in an array is called full initialization.

#include <stdio.h>
int main()
{
    //创建一个数组并将它完全初始化
    int arr[5] = {1, 2, 3, 4, 5};
    return 0;
}

We can open the monitoring window through debugging in vs to observe the value stored in the arr array:

000a08a3629d492bb26421c6c57caf09.png

 We can see that all the elements in the arr array are the values ​​initialized when we created the array.

Note: The entire array must be initialized at the same time as the array is created. If you want to initialize the entire array after creating it. Will cause illegal access.

For example: first create an integer array int arr[3];

           Then initialize the arr array as a whole arr[3] = 0;

This will not work. The compiler will think that you are assigning a value to the element with subscript 3, but the largest subscript in the arr array is 2; this will cause an illegal access and the compiler will report an error.

7.2.2 Incomplete initialization

When creating an array, initializing some elements in the array is called incomplete initialization.

Next we create an array and partially initialize its elements:

#include <stdio.h>
int main()
{
	//创建一个数组并将它不完全初始化
	int arr[10] = { 1,2,3,4,5 };
	return 0;
}

Now we create an array arr and initialize some of its elements. I believe some friends will ask now, your array has only initialized some elements, so what are the other elements that have not been initialized?

With this question, we turn on debugging again and observe the contents of the arr array through the monitoring window:

c081479e49ca45a9a8b0fa5540839ad0.png

 We can see: uninitialized elements are 0.

Someone asked when seeing this, the uninitialized elements of your integer array are 0; what about the character array and floating point array?

Next, we create an incompletely initialized character array to observe what is stored in it:

 2cadb3a35ace4cf292f4ced00328a944.png

We can see that the uninitialized element in the character array is '\0'. But the ASCII code value of '\0' is 0, so '\0' is also 0.

Summary: In an incompletely initialized array (no matter what type of array), the uninitialized elements are all 0.

7.3 Array subscripts

C language stipulates that each element of the array has a subscript, and the subscript starts from 0. Arrays can be accessed through subscripts.

If the number of elements in the array is n, then its subscript range is 0~n - 1.


8.static

Before explaining static, let's first understand the link attributes and storage types.

8.1 Connection properties:

The link attribute is related to the linking process of each object file and function in C language, and is used to determine whether the identifiers of different files (that is, the various names defined in the program, including variable names and function names) are the same entity. More generally, it means whether the variable and function declarations in two different files point to the same entity. For example: files a and b declare
variable c at the same time, and the link attribute specifies whether the two variables c are the same c.
Simply put, the function of the link attribute is to allow you to decide in file a whether to access the variables and functions in file b.

8.2 Classification of connection attributes: 

Link attributes are divided into three categories: external link (external), internal link (internal), and no link (none).

No linkage (none) : An identifier without a linkage attribute is always treated as a separate entity, that is, multiple declarations of the identifier are treated as different independent entities . Such as: local variables, function labels, function parameters, etc.

External link (external) : The identifier belonging to the external link attribute represents the same entity no matter how many times it is declared and located in several source files . For example: global variables and functions without static modification.

Internal linkage (internal) : All declarations of an identifier belonging to the internal linkage attribute in the same source file point to the same entity , but multiple declarations located in different source files belong to different entities . For example: global variables and functions modified by static.

8.3 Storage types

The storage type of a variable refers to the memory type that stores the variable value. The storage type of a variable determines when the variable is created, when it is destroyed, and how long its value is retained.

There are the following storage types of variables in C language:

1. auto automatic variable 2. register register variable   

3. static static variable 4. extern external variable

1. auto variable:

The default type of a variable declared in a code block is automatic, which means it is stored on the stack and is called an auto variable. The keyword auto modifies this storage type, but it is rarely used. Because the variables in the code block are automatic variables by default.

Automatic variables are created when the program executes to the code block in which the automatic variables are declared. The automatic variables are destroyed by themselves when the execution flow of the program leaves the code block.

2. register variable:

The keyword register can be used in the declaration of automatic variables, indicating that they should be stored in the machine's hardware storage rather than in memory. Such variables are called register variables. The creation time and destruction time of register variables are the same as automatic variables.

Variables of this type of storage are more efficient to access than variables stored in memory. But the compiler does not necessarily pay attention to the register keyword. This keyword just gives the compiler a suggestion, and it will judge whether to store the variable in a register based on the actual situation.

3. static variables

After adding static to a variable declared in a code block, its storage type will automatically change from static to static. Variables with static storage types persist throughout program execution. However, the scope of the variable is not modified and it can still only be accessed within the code block. 

4. extern external variables

Declaring a global variable as an extern variable in other source files can extend the scope of the global variable to the file in which it is declared. Its essential function is to extend the scope of the global variable. External variables, like static variables, will not be destroyed until the end of the program.

8.4 static modification 

 In C language, static is used to modify variables and functions:

static modified local variables - called static local variables

Let’s explain it through an example:

#include <stdio.h>
void test()
{
    static int a = 0;
    ++a;
    printf("%d ", a);
}

int main()
{
    int i = 0;
    while(i<10)
    {
        test();
        i++;
    }
    return 0;
}
    

You might as well guess what the result of running this string of code is? Is it outputting 10 1s? Let's run it through vs to see the results:

eedb1eebeff9401490865ca9b5c1a3eb.png

 Why is the output result from 1 to 10? Through the code, we can find that the front of variable a is modified by static.

When our local variables are modified by static, as we said before, the local variables have no connection attributes and the storage type is auto. After static modification, its storage type changes from automatic variable to static variable, but its link attributes and scope are not affected. Its life cycle also becomes the life cycle of the entire program.

2. Modify global variables - called static global variables

Let’s look at a bunch of code:

9057cd176488488f84841a4730a8b0eb.png

  As we said before, global variables can be accessed in different source files after being defined. Now let’s add static in front of the global variable to see what the results are:

c8a490c0e4014d4f8552c07329552080.png

 We can see that the compiler reported an error to us, saying that there was an external command that could not be parsed.

When static is added before the declaration of our global variable, static changes its connection attribute (from external to internal), but the storage type of the variable will not be affected. This makes global variables inaccessible only within the source file in which they are located.

3. Modified functions - called static functions

When a function is modified statically, like a global variable, the connection attribute changes from an external link attribute to an internal link attribute. The function can only be accessed in the source file where it is located.

Guess you like

Origin blog.csdn.net/m0_74459723/article/details/127468281