C language rough notes (2) - constants

constant

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

  1. literal constant
  2. Constant variable modified by Const
  3. #define defined identifier constant variable
  4. enum constants

 Literal constants

For example, if you directly type "1", nothing else is the literal meaning.

 Constant variables modified by Const

Const int num=4
Num is a constant variable modified by Const. It has constant attributes, constant characteristics and - cannot be changed, but it is still a variable in essence.
Insert image description here

The above is a simple example to help understand. When const modifies num to make it a constant variable, if you assign a value to it, an error will be reported. Because num is already a constant variable and has constant attributes, its value cannot be changed, but be clear. It is essentially a variable.
Its essence proves that the variables are as follows:
first define a constant variable num modified by const. Then write it into the array

Insert image description here
Insert image description here

At this time, if you type the subscript 0 in the array, you will find that it reports an error, and in the square brackets, you are asked to fill in the constant. At this time, what we lose is the const-modified num, which means that num is not a constant.
Insert image description here
Insert image description here

When the value in the square brackets is changed to the literal constant 8, it is successful.
From this, you should also know that when determining the size of an array, constants should be written in the brackets in "[]". When can variables be used within square brackets of an array?
When you represent an item in an array, you can use a variable in the square brackets to represent the subscript of the array.
as follows:

Insert image description here

 #define defined identifier constant

Insert image description here

 Enumeration constants

Enumeration and enumeration
enumeration keyword enum
Insert image description here

Each enum constant (here refers to small, middle, big) has a corresponding fixed value. So they are called "enumeration constants", which can also be understood as giving them a name. That name is essentially equivalent to a number.
The following helps to understand: define a variable s created by the enumeration and make it equal to small. and is equivalent to s=0;
Insert image description here

Variables created by enumeration types can be changed, but enumeration constants cannot be changed.
Um? ? ? ? ? What does it mean? ? ? ? ? ? ?
 The variables created by the enumeration type can be changed
Insert image description here

 But enumeration constants cannot be changed
Insert image description here

Small is already an enumeration constant, you can't assign it as a variable anymore.

Guess you like

Origin blog.csdn.net/m0_53126906/article/details/113770421