C language variable definition and types [local variables, global variables, formal parameters]

Detailed explanation of variables [local variables, global variables, formal parameters]

Definition of variables

What is a variable?
  For the interpretation of general variables, we can literally understand the amount that allows the value to be changed, which is a name that is easy to understand and use for a certain object value. It may be a bit unclear to say that, for example, our ID card is a kind of variable to some extent. Each ID number (variable) represents an exact person (value), and people can have different values. Development, the outside world can have different influences on it, but once we mention this ID number, it is the only point to this ontology. In our C language, we often understand a variable as a named location in memory that stores a value that can be modified.

变量的定义即声明,在定义时一般遵循以下规则:

 type variable_list;

       The type refers to the valid data type with arbitrary modifiers, and the variable_list is composed of one or more identifier names separated by commas. What are modifiers? (The content is explained later in this article.)

       For example, the following definitions are all legal:

int a;
int a,b,c;
short int num;
long int b_num;

type of data:

       Common data types are commonly used in six types: char, short, int, long, float, and double. Different data types occupy different memory sizes. Of course, the space allocated for different data types in different systems is also different. It is recommended to If you don't know, use sizeof to test it in advance. Here is an example:
For example, on different systems in a 32-bit system,
char——1 byte bit;
short——2 byte bit;
int——4 byte bit;
long——4 byte bit;
float ——4 byte bits;
double——8 byte bits.

Modifier

The existence of modifiers can be used to change the meaning of basic data types to accurately change the requirements of variables in different environments. Common modifiers are: void, signed, unsigned , Long (long integer) and short (short integer). The types of numbers represented by different modifiers are different, as well as the size and scope of the space occupied by them. This must be paid attention to when defining variables and analyzing programs.
[Note]
1. Variables must be declared before they are used (tell everyone that this thing exists and has been settled somewhere in the memory, just find it if you have something to do).
What happens if I want to use it directly? for example:

num = 20;
	printf("%d", num);

Then at this time you will find that the program cannot run, and you will be prompted with an error message;

 error C2065: “num”: 未声明的标识符

2. At the same time, variables with the same name cannot appear in the same scope (isn't it troublesome to appear true and false Monkey King), and the same variable definition appears, the program will produce errors. E.g:

	int a;
	int a;

This code does not conform to the correct definition specification, and the compiler will report an error:

error C2086:int a”: 重定义

3. It is stipulated in C language that variables can only be defined once in a program, but they can be declared multiple times.

Classification of variables

Variables can be roughly divided into three categories according to the location and scope of their declaration:
Inside the function-local variables
In the definition of function parameters-formal parameters
Outside all functions-global variables

Local variable

      A local variable is a variable declared inside a function. The scope of a variable is local as its name. It can only be used inside the code block where it is declared. At the same time, the local variable only enters the code block where it is located. Generated, it will be automatically destroyed after the end of the code block in this area. In other words, after running out of this part, no matter whether it is redefinition or other kinds, it has nothing to do with the variable. The most notable feature of a code block is the area that starts with the left curly brace and ends with the right curly brace.
      ; For example, the following program:

void func1()
{
    
    
	int num1, num2,result;
	result = num1 + num2;
	
}
void func2()
{
    
    
	int num1, num2, result;
	result = num1 + num2;
}

The above two functions belong to two different code blocks. Therefore, the variables num1 and num2 defined in func1 do not conflict with the local variables in result and func2. When using it here, be sure to note that when exiting the function, the local variables defined in the function will be destroyed immediately and can no longer be used directly.

Formal parameters

      When using a function, if you need to use an argument, you must declare the variable that will accept the value of the argument. This kind of variable is called the formal parameter of the function, and all its properties are roughly the same as local variables. The difference is that their declaration appears in the parentheses after the function name. The general formal parameter is just a copy of the value of the incoming parameter. Using this value does not affect the address in its own address. This is a special need to pay attention; but if you are using a pointer type parameter, you can perform the address Operation, here needs special attention.
      For example, the following code operates on the variable a:

#include<stdio.h>
void func1(int a)
{
    
    
	a = 100;
}
int main()
{
    
    
	int a = 20;
	printf("a=%d\n", a);
	func1(a);
	printf("a=%d\n", a);
}

The above program finds that the value of a has not changed in the output of the operation, which confirms the foregoing that the formal parameter is just a copy of the value of the incoming variable and does not affect the variable itself.
The output is as follows:
Insert picture description here
What should we do if we want to change the value of variable a? Refer to the following procedures:

#include<stdio.h>
void func1(int *p)
{
    
    
	*p = 100;
}
int main()
{
    
    
	int a = 20;
	printf("a=%d\n", a);
	func1(&a);
	printf("a=%d\n", a);
}

The directly passed in by the above program is the address of the address variable, which operates on the value in the address, so it can be changed. Output! is as follows:
Insert picture description here

[Therefore, you must pay attention to this type of problem during operation.

Global variable

In contrast to local variables, global variables can be used in the entire program, can be used in any code segment, and their value is unique in the entire program. When in use, if you redefine or copy global variables in the code segment, the value of the global variables will be automatically hidden, and the most recent set of data will be used. For example, the following program:

int number = 100;
int main()
{
    
    
	int b;
	b = number;
	printf("%d\n", b);
}

In this program, there is no related definition of number in main, but it can be called directly. This operation can be used in all code blocks of the program. The output is as follows:
Insert picture description here
In addition to the following situations:

#include<stdio.h>
int number = 100;
int main()
{
    
    
	int b;
	int number = 200;
	b = number;
	printf("%d\n", b);
}

In this program, the number variable is redefined in the main function. At this time, the number variable in this block is used in the nearest principle, and the global variable is automatically hidden.
The experimental results are as follows:
Insert picture description here

Guess you like

Origin blog.csdn.net/dream_i_success/article/details/109598723