How to define constants in the C language?
In C language, constants can be defined in the following way:
- Integer constants can be in decimal, octal, or hexadecimal form. For example:
int dec = 10; // 十进制常量
int oct = 012; // 八进制常量,等同于10
int hex = 0xA; // 十六进制常量,等同于10
- Floating-point constants: they can be floating-point numbers in decimal or exponential form. For example:
float f = 3.14; // 小数形式的浮点数常量
float e = 2.7e-3; // 指数形式的浮点数常量,等同于0.0027
- Character constants: characters enclosed in single quotes. For example:
char c = 'a'; // 字符常量
- String constants are sequences of characters enclosed in double quotation marks. For example:
char str[] = "Hello, world!"; // 字符串常量
- Define
#define MAX_SIZE 100 // 符号常量
In programming, the value of constants is fixed and cannot be changed. Therefore, once constants are defined, their value will remain unchanged.