How are constants defined in the C programming language?
In the C language, the #define preprocessor directive can be used to define constants. Constants are named following the same rules as variables, typically using uppercase letters.
The syntax for defining constants is as follows:
#define 常量名 常量值
For example, defining an integer constant MAX_NUM as 100.
#define MAX_NUM 100
In the program, you can directly use the constant name MAX_NUM to represent the constant value of 100.
In addition, the C language also provides the const keyword to define constants. Constants defined using the const keyword are typically used within functions, and the syntax for defining them is as follows:
const 数据类型 常量名 = 常量值;
For example, define an integer constant MAX_VALUE as 100.
const int MAX_VALUE = 100;
Constants defined using the const keyword have a scope limited to the block in which they are defined (blocks can be functions, loops, conditional statements, etc.).