The past and present of variables in C Language Kingdom Adventure

Kingdom Adventures Series

Article Directory (2)


foreword

1. What are variables?

         2. How to define variables?

Note: When you assign a decimal, the compiler will naturally think that you are a double type, so when you use the float type, you need to add an f after the decimal, and then the type of the decimal is the float type.

Third, the naming of variables

Basic requirements for naming variables:

 Examples of variable naming:

Fourth, the creation of variables

Five, variable verification

Six, global variables and local variables

Seven, the scope of variables

1. What is the scope:

2. The scope of global variables and local variables

  2.1, the scope of a local variable is the local scope where the variable is located.

Here are three examples: code, comments, and results running graphs to demonstrate

Example 1:

Example 2:

Example 3:

   2.2, the scope of global variables is the entire project

3. How can global variables be used?

 Eight, the life cycle of variables

1. What is the life cycle:

Life cycle: The life cycle of a variable refers to a period of time between the creation of the variable and the destruction of the variable.

2. The life cycle of local variables

   2.1 The life cycle of a local variable begins when it enters the scope, and ends when it exits the scope.

 3. The life cycle of global variables

        3.1 The life cycle of global variables is the life cycle of the entire program.

Summary of the life cycle of global variables:

Summarize


foreword

Adventures in the Kingdom of C Language is the journey of our journey of learning C language and growing up from a novice to a master. In this chapter, we will explore the knowledge level of variable variables.



1. What are variables?

Variables are quantities that describe changes, and some values ​​are always changing

For example: age, weight, salary, etc.,

In the C language, those variable quantities are represented by variables.

2. How to define variables?

type name; Explanation: A data type is followed by a name, the basic form of variable creation

Examples are:

int whether;

char c;

Note: When you assign a decimal, the compiler will naturally think that you are a double type, so when you use the float type, you need to add an f after the decimal, and then the type of the decimal is the float type.

Third, the naming of variables

Basic requirements for naming variables:


 Examples of variable naming:

int 2*3; because * is not among numbers, letters, and underscores, it does not work

int 2b; not possible because the beginning cannot be a number

int _2b; ok because the beginning can be an underscore

Length cannot exceed 63 characters

int _2B; Yes, but it is different from the above int _2b, so it is case sensitive

Keywords cannot be used in C language

int char ; is not allowed The name of the variable cannot be a keyword

The name of the variable should be as meaningful as possible, don’t make fools of it, and don’t let people not understand your variable name

Fourth, the creation of variables

We use code plus comments to explain the variables of each type created

int main()
{
	int zhangsan = 20;//我想创建一个整型变量,
    //名字叫zhangsan这样就可以了,创建的时候我想给他赋一个20,在名字后面加一个=20就可以了
	int num = 0;//创建一个整数变量叫 num它的值是0;这样也可以
	char ch;//创建一个char类型的变量ch
	double d;//创建一个double 类型的变量叫d
	return 0;
}

Five, variable verification

We use code plus comments to explain the variables of each type created

int main()
{
	//为什么叫变量呢,变量是在说明他的值可变
	int  a = 10;
	printf("a=%d\n", a);//我们来打印验证一下,说明a确实可变
	a = 100;
	printf("a=%d\n", a);//加个a=号表示的是在占位符前面加一个修饰,占位符会被后面的值替换掉
	return 0;
}

Six, global variables and local variables

Global variables: The variables defined outside {} are global variables, and all global variables can be used. You can use it in whatever state you want to achieve

Local variables: variables defined inside {} are local variables, which can be used locally

When a local variable and a global variable can be used in one place, that is, local precedence

Specific examples are directly annotated in the code, and the result operation diagram is understood

//全局变量:在{}外边定义的变量就是全局变量,全局变量的全都可以使用
//局部变量:就是{}内部定义的变量就是局部变量,在局部内可以使用
//当一个局部变量和一个全局变量在一个地方都可以使用的时候就是,局部优先
int a = 100;//当我们把变量放到大括号外面的时候被称为全局变量
int main()
{
	printf("全局变量a-->%d\n", a);
	int a = 10;//在括号里面的时候被称为局部变量
	{
		int b = 1000;
		printf("局部变量b-->%d\n", b);//局部变量b的使用
	}
	printf("局部优先a-->%d\n", a);//虽然有全局变量a=100,但是我们有着局部优先原则,所以a打印为10
}

Seven, the scope of variables

1. What is the scope:

Scope (scope) is a programming concept. Generally speaking, the name used in a piece of program code is not always valid/available, and the code scope that limits the availability of this name is the scope of this name.

Explanation: Generally speaking, this variable is where it can be used and where it is used.

2. The scope of global variables and local variables

  2.1, the scope of a local variable is the local scope where the variable is located.

Understanding: The local scope we are talking about is the local scope backwards from its definition, and the scope of local variables is its scope . Local variables are like your own bicycle, which can only be ridden by people in your family

Here are three examples: code, comments, and results running graphs to demonstrate

Example 1:

int main()
{
	{
		int a = 10;//10之所以可以正常打印,
                  //就是因为在这个大括号里面创建的变量在这个大括号里面
                 //是可以在大括号里面使用是没有问题的
		printf("%d\n", a);
	}
}
//a变量只能在自己所在的大括号里面使用,大括号就是他的作用域,

 Example 2:

int main()
{
	{
		int a = 10;//在大括号里面定义的a只能在在里面看到出了大括号就看不到了
	}
	printf("%d\n", a);//当我们把这个printf()放到外面,就会报错说是未定义的标识符a
}

Example 3:

int main()
{
	int b = 100;//在它所在的范围内,定义的代码往后的都可以使用
	{
		int a = 10;
		printf("%d\n", b);
	}
	printf("%d\n", b);//这个b是定义在大括号内部的,你在任意位置都可以使用
	return 0;
}


   2.2, the scope of global variables is the entire project

Understanding: The scope of global variables is very wide, just like a shared bicycle on the road, anyone can use it.

Code, comments, and resulting run graphs can aid in understanding:

int a = 100;//这是一个全局范围内的变量,在main()函数里可以使用,在自定义的函数里面也可以使用
void test()//我们单独在自定义的一个函数里面打印a,
{
	printf("test()--->%d\n", a);
}
int main()
{
	printf("main()-->%d\n", a);
	test();//调用这个test函数
	return 0;
}

 

3. How can global variables be used?

Variables defined in other files can be used through the extern declaration,
but multiple a cannot be defined in other files of a project, because multiple a will cause the scope of redefining
global variables to be the entire project, as long as it is legal It can be used in all grammatical situations,
which further shows that the scope of global variables is very wide
 

Code, comments, and resulting run graphs can aid in understanding:

Because our code verification requires the use of code in two files, we divide the code into two sections and attach screenshots of the two sections of code

// 通过extern 的声明就可以使用其他文件的定义的变量
// 但是在一个项目的其他文件中不能定义多个a,因为存在多个就会造成重定义
//全局变量的作用域是整个工程的,只要是合法的语法情况下都可以使用
//更加说明了全局变量的作用域是非常广的



extern int a;//extern是用来声明外部符号
//声明来自其他文件的一些符号
//声明一下我有一个变量a,它的类型是整型,它来自外部
void test()
{
	printf("test()--->%d\n", a);
}
int main()
{
	printf("%d\n", a);
	test();
	return 0;
}
int a = 10;
//我们在add.c里面创建一个全局变量,我们能不能在test.c里面使用呢

 

 Eight, the life cycle of variables

1. What is the life cycle:

Life cycle: The life cycle of a variable refers to a period of time between the creation of the variable and the destruction of the variable.

2. The life cycle of local variables

   2.1 The life cycle of a local variable begins when it enters the scope, and ends when it exits the scope.

Code, comments, and resulting run graphs can aid in understanding:

int main()
{
	{
		int a = 100;//创建一个a变量等于100,代码从上往下执行执行到a的时候才有必要创建,
		//前面a都不存在所以没有存在的道理,到int a这一行才开始创建,生命才开始
		printf("在生命周期内打印的a-->%d\n", a);//打印我们的a
	}//因为我们是在大括号内部打印,没有出a的生命周期所以可以打印成功
	return 0;
}

 3. The life cycle of global variables

        3.1 The life cycle of global variables is the life cycle of the entire program.

Code, comments, and error screenshots can help understand:

int main()
{
	{
		int a = 100;//这种局部变量是进入它的作用域,创建,生命周期开始,离开作用域生命周期结束
		//出了作用域就不能使用了,所以就结束,因为每一个变量都需要占用内存,当局部变量出了作用域,
		//这个变量就不能用了,还有必要让他进行占用内存吗?/没必要,所以它的生命周期就结束了
	}
	printf("%d\n", a);//当我们运行的时候,就会报错
	//同时我们出了大括号就不能使用a了,
	//因为a这个局部变量不能在被使用,所以a变量就被销毁了
	return 0;
}

Summary of the life cycle of global variables:

The life cycle of the global variable is the same as the life cycle of the program.
The program is executed from the first line of the main() function until the end of the main() function. In the entire life cycle of the main() function, the global variable All can be used, which means that all global variables exist, so the life cycle of the main() function is the life cycle of the program. If the global variables can be used in these cycles, then the life cycle of the global variables is actually the same as the life of our program. The period is the same
.

Summarize

The above is what I want to talk about today. This article only briefly introduces the basic concept and verification of variables, which can make your adventure in the C language kingdom more interesting and fulfilling.

Guess you like

Origin blog.csdn.net/weixin_73466540/article/details/131382903