C language constants (integer, real, character, string, symbolic constants)

For C language constants, the article has a detailed explanation. Since the article is too long, you can refer to the following mind map and table of contents. You can choose the content you need to read through the table of contents.

direct constant

1. Integer constant

Integer constants are integers, including positive integers, negative integers and zero. In C language, integer constants can be represented in decimal, octal, and hexadecimal.

Decimal representation:

It consists of numbers 0-9, plus and minus signs, such as 369, -663, etc.;

Octal representation:

It is prefixed with 0 (number 0), followed by numbers 0-7, such as 0163, 036, etc.; octal numbers are generally unsigned numbers. For example, 0192 and -011 are illegal octal constants.

Hexadecimal representation:

It is prefixed with 0x or 0X, followed by numbers 0-9 and letters AF (letters can be uppercase or lowercase), such as 0x12cd, 0X6Fa, etc. Hexadecimal numbers are generally unsigned numbers.

Notice:
  1. Adding a letter u or U after an integer constant is considered to be an unsigned int type, such as 246U.

  1. Adding a letter l or L after an integer constant is considered a long int type, such as 12L.

2. Real constants

Real constants are real numbers, also known as floating point types. In C language, real numbers can only be represented in decimal form. There are two ways to represent real numbers: decimal form and exponential form.

    • Decimal form

It consists of an integer part, a decimal point, and a decimal part. When the integer part or the decimal part is 0, it can be omitted, but the decimal point cannot be omitted. For example, 12.345, -.123, etc. are all correct real numbers.

    • exponential form

It consists of the mantissa part, the letter E or e and the exponent part. The format is: + (-) mantissa E exponent.

For example, 1.23E-2, 3.14E+3, etc., the values ​​they represent are 1.23 X 10^(-2), 3.14 X10^3 respectively.

Notice
  1. When expressing a real number in decimal form, there must be a decimal point and at least one digit before and after the decimal point.

  1. When expressing a real number in exponential form, there must be numbers before and after the letter E (or e), and the exponent part can only be an integer. For example, 12.-E3, 1.5E, and E6 are all incorrect real numbers.

In C language, real type constants default to double type real numbers. If the letter f or F is added after the number (such as 1.65f, 654.87F), it is considered to be a float type real number.

3. Character constants

A character constant refers to a single character, represented by a pair of single quotes and the character enclosed by its parentheses. For example, 'a', 'B', '=', '$', and '?' are all legal character constants. In C language, character constants are as follows

Features:
  1. Character constants can only be enclosed in single quotes, not double quotes or other brackets;

  1. Character constants can only be single characters, not strings.

  1. Characters can be any characters in the ASCII character set.

When using character constants, each character occupies one byte in memory to store its ASCII code value. Therefore, character constants in C language have numerical characteristics and can participate in operations like integers, which is equivalent to operating on the ASCII code of the character.

4. String constants

A string constant is a sequence of zero or more characters enclosed by a pair of double quotes "", such as "HelloWorld".

Space characters, escape characters, and other characters can be used in strings, as well as text symbols such as Chinese characters. Such as: "china", "Hello Visual C", "\t Visual C", etc. Null characters can also be used in strings, such as: "" means an empty string and does not contain any characters.

1. String constants occupy a continuous storage unit in the memory. The system automatically adds '\0' at the end of each string as the end mark of the string. Therefore, a string composed of n characters is stored in the memory. It takes up n+1 bytes of space.

You can use the sizeof operator to calculate the memory space occupied by a string.

For example, the string constant "Hello\nWor" occupies 10 bytes of memory space, and the storage diagram is as follows:

H

e

l

l

o

\n

W

o

r

\0

2. The length of the string is equal to the number of valid characters contained in the string. For example, the length of "HelloWorld" is 10. In a string, if '\0' is encountered ('\0' is the string terminator), the string is considered to end. For example, the length of "Hello\0World" is 5. If there are escape characters in the string, one escape character is used as one character, for example, the length of "HelloWorld\t" is 11. You can use the strlen() function to calculate the string length.

Notice

The storage situation of character constants and string constants in memory is different. For example, '6' occupies 1 byte in memory, and its ASCII code is stored, while "6" occupies 2 bytes in memory, one One byte stores '6', and the other byte stores "\0". You can assign a character constant to a character variable, but you cannot assign a string constant to a character variable. In C language, since there is no string type variable, strings are generally solved with character arrays.

symbolic constant

In C language, an identifier can be used to represent a constant, which is called a symbolic constant. Symbolic constants must be defined before use, and their general form is:

#define identifier constant value

Among them, #define is a compilation preprocessing command, called a macro definition command. Its function is to define the identifier as the constant value after it. The identifier is called a symbolic constant. A #define command can only define one symbolic constant. If you want to define multiple symbolic constants, you need to use multiple #define commands.

The use of symbolic constants can provide a lot for writing programs

benefit:
  1. Increase the readability of the program: you can know the meaning of the constant through the identifier, that is, you can know the meaning by its name.

  1. Improve the maintainability of the program: Symbolic constants make it easy to modify constants. You only need to modify them at the definition of the constant.

  1. Simplify program code: Symbolic constants can simplify repeated input of complex expressions.

  1. Convenient array definition: you can use an integer symbolic constant as the length of the array.

Guess you like

Origin blog.csdn.net/m0_75115696/article/details/128746130