The Four Guardians of the Constant in C Language Kingdom Adventure

Kingdom Adventures Series

Article Directory (3)


foreword

1. What are constants?

2. The first guard of constants: literal constants

1. What is a literal constant?

3. The second guard of constants: constant variables modified by const

1. What is a constant variable modified by const?

2. Prove that the constant variable modified by const is still a variable but has constant properties

​edit

4. The third guard of constants: identifier constants defined by #define

1. What is an identifier constant defined by #define?

5. The fourth guard of constants: enumeration constants

1. What is an enumeration constant?

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 constants


1. What are constants?

Some values ​​in life are constant (such as: pi, gender, ID number, blood type, etc.)

Use constants to express those invariable quantities in C language

2. The first guard of constants: literal constants

1. What is a literal constant?

Literal values ​​written directly, these values ​​are called literal constants

Example understanding:

100

20.0

3.14

‘a’

These numbers and characters are what we call literal constants

3. The second guard of constants: constant variables modified by const

1. What is a constant variable modified by const?

const means constant attribute, which can modify variables

Constant means that it is a value that does not change

The variable is variable, but the length is constant, isn't it contradictory?

Understanding: Just like a person who broke the law in prison, he is still a citizen of the People’s Republic of China in prison, and he
still has the attributes of a people, but because he broke the law, we locked him up, and he It has another layer of attributes, he is restricted in freedom

Next, let's directly add comments to the code with specific examples, and understand the result running diagram:

int main()
{
	//就和监狱中犯了法的人一样,在监狱里面,他们还是中华人民共和国的一个公民,
	//他还是有这样一个人民的属性的,但是因为他犯法了,我们把他关起来了,
    //他就具有了另外一层属性,他被限制了自由
	const int a = 10;//现在在int a前加一个const ,a本来里面放的是10
	//我们把a叫做const修饰的常变量
	//const修饰a的时候也是限制了a的自由,让a不能在被改了,但是他本质上还是一个变量
	printf("a=%d\n", a);
	a = 100;//当我们再次把a修改为100的时候就会报错,这个错误是啥意思呢,
    //就是当你去修改这个a的时候,抱歉a不能修改
	//const修饰a的时候让a具有了常属性,但是本质上a还是一个变量,
    //只不过是被const从语法上加了一个限制,让它不能在被改了
	//const放在那就是告诉你a的值放在那,你以后不要在改了
	//const就是在语法层面上加一个限定,说一旦一个变量被const修饰之后,你就不能去改它,
    //你如果去改它就是违背规则
	printf("a=%d\n", a);
	return 0;
}

2. Prove that the constant variable modified by const is still a variable but has constant properties

Next, let's directly add comments to the code with specific examples, and understand the result running diagram:

//C语言里面一个叫数组,先看懂就行
int main()
{
	//我们在创建数组的时候需要指定数字大小
	//比如说,我们创建个数组叫arr大小给个10
	//数组在创建的时候要求arr的大小是一个常量
	int arr[10] = {0};//这个10就在告诉我,数组是10元素的,数组里面可以放10个值的
	//int n = 10;//我们刚刚使用n,n是个变量不行,
	const int n = 10;//当我们加一个const呢,还是不行,会报错,
	//所以现在n即便是被const修饰了还是不行,因为它还是一个常变量,
	//它是具有一定常属性的变量,它本质上是一个变量,所以不能放在这里
	int arr2[n] = { 0 };//当我们去编译的时候,它就报错,它提醒你表达式必须含有常量值,
	//也就是说你只能拿常量来指定arr的大小
	//在c++中把const修饰的n当常量来处理的,这是c和c++在这个语法细节上的差异
	return 0;//他是个常变量,他本身具有常数的属性,但是他还是一个变量
}

4. The third guard of constants: identifier constants defined by #define

1. What is an identifier constant defined by #define?

Format: #define name assignment

#define M 100   
We define an M whose value is 100. At this time, this M is the identifier constant we defined

Does this identifier constant count as a constant? Calculate

Can M be used as the number of its array, of course, because M at this moment is a constant and it is an identifier constant

Next, let's directly add comments to the code with specific examples, and understand the result running diagram:

#define M 100
我们定义一个M它的值是100,这个时候这个M就是我们定义出来的标识符常量
int main()
{
	int arr[M] = { 0 };//M作为它的数组个数行不行呢,当然可以,
    //因为此时此刻的M就是一个常量它是一个标识符常量
	//这就是我们定义的一个标识符常量
	//你以后在使用M的时候M的值就是100
	//那我们怎么用呢
	//我们能不能把这个M的值打印出来呢
	printf("标识符常量M-->%d\n",M);//当这个地方出现M的时候就会被替换成100
	int a = M;//这个地方有个a,我们将M的值赋予给a,你在这里在打印a都是没有问题的
	printf("标识符常量M被赋予给a后的a值-->%d\n", a);
	return 0;
}

 

5. The fourth guard of constants: enumeration constants

1. What is an enumeration constant?

What is an enumeration constant.
Is the enumeration constant listed one by one?
There are some things in our life that can be listed one by one,
and the values ​​of some data can be listed one by one
, for example: three primary colors: red, green, blue
gender: male, female, confidentiality
week: 1 2 3 4 5 6 7Through
these examples, we can know that some values ​​in our life can be listed one by one. In
C language, there is a type called enumeration, which
can list these possible values ​​one by one.
Enumeration keyword: enum

Next, let's use specific examples to add comments directly to the code to help understand:

//这就是我们创造的自定义的一个类型叫枚举类型叫 enum Color,
//这个颜色的取值只有三种放在下面的大括号里面
enum Color //定义颜色的这样一个类型
{
	//枚举类型里面列出来的值叫枚举常量
	//就是这个枚举类型可能的取值就是这三种,所以我可以将她一一列举出来
	RED,  //第一个常量是0,默认从0开始依次递增的
	GREEN,//1
	BLUE  //2
};
//未来呢,假设我想表示一个颜色,我会怎么做呢?
int main()
{
	enum Color c =RED ;//可以用这个自定义类型创建一个c,
	//用这个c来表示什么样的颜色,而c的取值只有三种,RED,GREEN,BLUE,
	//你可以给c用这三种变量给它赋值
	return 0;
}

For example, print the default value of the enumeration constant

Next, let's directly add comments to the code with specific examples, and understand the result running diagram:

enum Sex//enum sxe是一个类型,枚举类型里面列出来的值叫枚举常量
{
	MALE,//男,它们的值都是默认的//0
	FEMALE,//女//1
	SECRET//保密//2
};
int main()
{
	//但你想要表示一个性别的时候怎么用呢,
	//当你给一个人赋予性别的时候
	//enum Sex s = SECRET;//可以给小s赋值了
	//enum Sex s2 = MALE;
	printf("%d\n", MALE);//我们可以打印一下看一下
	printf("%d\n", FEMALE); 
	printf("%d\n", SECRET); 
	return 0;
}


 

Summarize

The above is what I want to talk about today. This article only briefly introduces the basic concepts of constants and the four types of constants, 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/131394233