当前位置:网站首页>Rough notes of C language (2) -- constants

Rough notes of C language (2) -- constants

2022-07-05 07:18:00 After reading thousands of books, you can pick them up and put

Constant

C The definition forms of constants and variables in language are different .
C Language constants are divided into the following :

  1. Literal constants
  2. Const Modified constant variable
  3. #define Defined identifier constant
  4. Enumeration constants

 Literal constants

For example, you can call directly “1” Nothing else, just literally

 Const Modified constant variable

Const int num=4
Num by Const Modified constant variable , Has constant properties , Have constant characteristics and —— Can't change , But it's essentially a variable .
 Insert picture description here

The above is a simple example to help understand , When const modification num Make it a constant variable , If you assign a value to it , Will report a mistake , because num Is already a constant variable , And have constant properties , Its value cannot be changed , But to be clear, its essence is variables .
The essential proof is that the variables are as follows :
So let's define one const Modified constant variable num. Then write it into the array

 Insert picture description here
 Insert picture description here

At this time, the subscript in the mapping array is 0 Then you will find that it reports an error , And in brackets , I want you to fill in the constant , At this time, we lose const Embellished num, shows num Is not a constant .
 Insert picture description here
 Insert picture description here

The value in the middle bracket is changed to a literal constant 8 Timely and successful .
Therefore, we should also know that the array is determining its size “【】” What should be written in brackets is a constant . When can variables be used in the brackets of an array ?
When you represent one of the arrays , Variables can be used in brackets to represent the subscript of the array .
as follows :

 Insert picture description here

 #define Defined identifier constant

 Insert picture description here

 Enumeration constants

Enumerate and enumerate one by one
Enumerate keywords enum
 Insert picture description here

Each enumeration constant ( Here it means small,middle,big) There is a corresponding fixed value . So they are called “ Enumeration constants ” It can also be understood as taking a name , That name is essentially equivalent to a number .
The following helps understand : Define a variable created by enumeration s, Make it equal to small. And equal to s=0;
 Insert picture description here

Variables created by enumeration types can be changed , But enumeration constants cannot be changed .
Um. ????? What do you mean by that? ???????
 Variables created by enumeration types can be changed
 Insert picture description here

 But enumeration constants cannot be changed
 Insert picture description here

Small It is already an enumeration constant , You can't say that you can assign it again as a variable .

原网站

版权声明
本文为[After reading thousands of books, you can pick them up and put ]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202140558148780.html