C Constants
Constants are fixed values that do not change during program execution. These fixed values are also calledLiterals。
Constants can be any basic data type, such as integer constants, floating-point constants, character constants, or string literals, as well as enumerated constants.
ConstantsThey are like regular variables, except that the value of a constant cannot be modified after definition.
Constants can be used directly in code, or they can be used by defining constants.
Integer constants
Integer constants can be decimal, octal, or hexadecimal constants. The prefix specifies the base: 0x or 0X indicates hexadecimal, 0 indicates octal, and no prefix indicates decimal by default.
Integer constants can also have a suffix, which is a combination of U and L. U stands for unsigned integer, and L stands for long integer. The suffix can be uppercase or lowercase, and the order of U and L is arbitrary.
Below are some examples of integer constants:
212 /* 合法的 */ 215u /* 合法的 */ 0xFeeL /* 合法的 */ 078 /* 非法的:8 不是八进制的数字 */ 032UU /* 非法的:不能重复后缀 */
The following are examples of integer constants of various types:
85 /* 十进制 */ 0213 /* 八进制 */ 0x4b /* 十六进制 */ 30 /* 整数 */ 30u /* 无符号整数 */ 30l /* 长整数 */ 30ul /* 无符号长整数 */
Integer constants can have a suffix to indicate the data type, for example:
Example
long myLong = 100000L;
unsigned int myUnsignedInt = 10U;
Floating-point constants
Floating-point constants consist of an integer part, a decimal point, a fractional part, and an exponent part. You can use decimal form or exponential form to represent floating-point constants.
When expressed in decimal form, you must include the integer part, the fractional part, or both. When expressed in exponential form, you must include the decimal point, the exponent, or both. The signed exponent is introduced with e or E.
Below are some examples of floating-point constants:
3.14159 /* 合法的 */ 314159E-5L /* 合法的 */ 510E /* 非法的:不完整的指数 */ 210f /* 非法的:没有小数或指数 */ .e55 /* 非法的:缺少整数或分数 */
Floating-point constants can have a suffix to indicate the data type, for example:
Example
double myDouble = 3.14159;
Character constants
Character constants are enclosed in single quotes. For example, 'x' can be stored incharin simple variables of a type.
A character constant can be an ordinary character (e.g., 'x'), an escape sequence (e.g., '\t'), or a universal character (e.g., '\u02C0').
In C, there are certain characters that have special meaning when preceded by a backslash. They are used to represent things like newline (\n) or tab (\t). The following table lists some of these escape sequence codes:
| Escape sequences | Meaning |
|---|---|
| \\ | \ character |
| \' | ' character |
| \" | " character |
| \? | ? character |
| \a | Alarm bell |
| \b | Backspace |
| \f | Form feed |
| \n | Newline |
| \r | Carriage return |
| \t | Horizontal tab |
| \v | Vertical tab |
| \ooo | Octal numbers of one to three digits |
| \xhh . . . | Hexadecimal numbers of one or more digits |
The following example shows some escape sequence characters:
Example
When the above code is compiled and executed, it produces the following results:
Hello World
The ASCII value of a character constant can be converted to an integer value through a type cast.
Example
int myAsciiValue = (int) myChar; // Convert myChar to ASCII value 97
String constants
String literals or constants are enclosed in double quotes" ". A string contains characters similar to character constants: ordinary characters, escape sequences, and universal characters.
You can use spaces as separators to break a very long string constant into multiple lines.
The following example shows some string constants. The strings shown in the following three forms are identical.
"hello, dear" "hello, \ dear" "hello, " "d" "ear"
String constants in memory are stored asnullTerminator\0End. For example:
char myString[] = "Hello, world!"; //系统对字符串常量自动加一个 '\0'
Define constants
In C, there are two simple ways to define constants:
- Usage#definePreprocessor: #define can define a constant in a program, and it will be replaced with its corresponding value at compile time.
- UsageconstKeyword: The const keyword is used to declare a read-only variable, that is, the value of the variable cannot be modified while the program is running.
#define preprocessor
The following is the form for defining constants using the #define preprocessor:
#define 常量名 常量值
The following code defines a constant named PI:
#define PI 3.14159When using this constant in the program, the compiler will replace all PI with 3.14159.
For details, see the following examples:
Example
When the above code is compiled and executed, it produces the following results:
value of area : 50
const keyword
You can useconstA prefix declaration specifies a constant of a given type, as shown below:
const 数据类型 常量名 = 常量值;
The following code defines a constant named MAX_VALUE:
const int MAX_VALUE = 100;When using this constant in the program, its value will always be 100 and cannot be modified.

The const declaration defines a constant within a statement:

For details, see the following examples:
Example
When the above code is compiled and executed, it produces the following results:
value of area : 50
Note that it is a good programming practice to define constants in uppercase letters.
Difference between #define and const
Both #define and const can be used to define constants. Which one to choose depends on specific needs and programming habits. Generally, it is recommended to use the const keyword to define constants, because it has the advantages of type checking and scope, while #define only performs simple text replacement, which may lead to some unexpected problems.
There are some differences between the #define preprocessor directive and the const keyword when defining constants:
Replacement mechanism:
#defineis a simple text replacement, whileconstis to declare a constant with a type.#defineConstants defined with #define are directly replaced with their corresponding values at compile time, whileconstThe defined constant will allocate memory at runtime and has type information.Type checking:
#defineno type checking is performed, because it is only a simple text replacement. However,constThe defined constant has type information, and the compiler can perform type checking on it. This can help catch some potential type errors.Scope:
#defineconstants defined with #define have no scope limitation; they are valid throughout the entire code after definition. However,constconstants defined with const have block-level scope and are only valid within the scope where they are defined.Debugging and symbol table: using
#defineConstants defined with #define do not have a corresponding entry in the symbol table, because they are only text replacement. However, usingconstconstants defined with const have corresponding entries in the symbol table, which helps with debugging and readability.