1. C language program type
1. C language program type
1.1. Integer quantity
Integer quantities include integer constants and integer variables. Integer constants are integer constants. In C language, there are three types of integer constants used: octal, hexadecimal, and decimal.
1.2. Integer constant
- Octal integer constants must start with 0, which is the prefix for octal numbers. The numerical value ranges from 0 to 7. Octal numbers are usually unsigned numbers
The following numbers are valid octal numbers:
015 (decimal 13) 0101 (decimal 65) 0177777 (decimal 65535)
The following numbers are not valid octal numbers:
256 (without prefix 0) 03A2 (including non octal digits) \ -0127 (with a negative sign)
- The prefix for hexadecimal integer constants is 0X or 0x. Its numerical values are 0-9, A~F or a~f
The following numbers are valid hexadecimal integer constants:
0X2A (42 in decimal) 0XA0 (160 in decimal) 0XFFFF (65535 in decimal)
The following numbers are not valid hexadecimal integer constants:
5A (without prefix 0X) 0X3H (containing non hexadecimal digits)
- Decimal integer constants do not have prefixes. Its number ranges from 0 to 9
The following numbers are valid decimal integer constants:
237 -568 65535 1627
The following numbers are not valid decimal integer constants:
023 (cannot have leading zeros) 23D (contains non decimal digits).
In the program, various base numbers are distinguished based on prefixes. Therefore, when writing constants, do not make mistakes with prefixes that may result in incorrect results.
- The suffix of integer constants is limited to 16 bits on machines with a word length of 16 bits, and the length of basic integers is also 16 bits, so the range of numbers represented is also limited** The range of unsigned integer constants in decimal is 0-65535, while signed numbers range from -32768 to+32767. The range of representation for unsigned octal numbers is from 0 to 0177777. The range of representation for unsigned numbers in hexadecimal is 0X0 to 0XFFFF or 0x0 to 0xFFFF. If the number used exceeds the above range, it must be represented by a long integer. Long integers are represented by the suffix "L" or "l". For example:
Decimal integer constant 158L (158 in decimal) 358000L (-3558000 in decimal)
Octal constant 012L (decimal 10) 077L (decimal 63) 0200000L (decimal 65536).
Hexadecimal integer constant 0X15L (decimal 21) 0XA5L (decimal 165) 0X10000L (decimal 65536).
There is no numerical difference between the long integer 158L and the basic integer constant 158. But for 158L, because it is a long integer, the C compilation system will allocate 4 bytes of storage space for it. For 158, as it is a basic integer, only 2 bytes of storage space are allocated. Therefore, attention should be paid to calculations and output formats to avoid errors. Unsigned numbers can also be represented by suffixes, with the suffix for unsigned numbers of integer constants being "U" or "u". For example, 358u, 0x38Au, and 235Lu are all unsigned numbers. The prefix and suffix can be used simultaneously to represent various types of numbers. If 0XA5Lu represents the unsigned long integer A5 in hexadecimal, its decimal value is 165.
1.3. Integer variable
Integer variables can be divided into the following categories:
Basic type,type descriptor is int, occupies 4 bytes in memory, and its value is a basic integer constant.
Short integer quantity,type descriptor is short. Occupying 2 bytes in memory, its value is a short integer constant.
Long integer,type descriptor is long, occupies 8 bytes in memory, and its value is a long integer constant.
unsigned,type descriptor is unsigned。
The unsigned type can also be matched with the above three types to form
(1)The unsigned basic type descriptor is unsigned int or unsigned.
(2)The unsigned short integer type descriptor is unsigned short
(3)The unsigned long integer type descriptor is unsigned long
The number of memory space bytes occupied by various unsigned types is the same as the corresponding signed types. However, due to the omission of the sign bit, it cannot represent negative numbers. The following table lists the number of memory bytes allocated for various integer types in ARM and their corresponding ranges of representation.
| Type descriptor | Range of numbers | Number of allocated bytes |
|---|---|---|
| int | -2147483648~2147483647 | ■■■■(4) |
| unsigned int | 0~4294967295 | ■■■■(4) |
| short | -32768~32767 | ■■(2) |
| unsigned short | 0~65536 | ■■(2) |
| long | -922337203685477808~922337203685477807 | ■■■■■■■■(8) |
| unsigned long | 0~18446744073709551615 | ■■■■■■■■(8) |
1.4. Explanation of integer variables
The general form of variable description is: type descriptor variable name identifier, variable name identifier; For example:
int a,b,c; (a,b,c are integer variables)
long x,y; (x,y are long integer variables)
unsigned p,q; (p,q are unsigned integer variables)
When writing variable descriptions, the following points should be noted:
Allow multiple variables of the same type to be specified after a type descriptor. Use commas to separate variable names. At least one space should be used between the type descriptor and the variable name.
The last variable name must end with a ";" sign.
Variable description must be placed before variable usage. Usually placed at the beginning of the function body.
int a, b;
short c;
short d = 100;
a = d - 20;
b = a + d;
c = a + b + d;
d = d - a + c - b;
1.5. Real constant
Real type is also known as floating-point type. Real constants are also known as real numbers or floating-point numbers. In C language, real numbers only use decimal. It has two forms:
In decimal form,is composed of digits 0-9 and a decimal point. For example, 0.0,. 25, 5.789, 0.13, 5.0300, -267.8230, etc. are all valid real numbers.
In exponential form,is composed of decimal numbers, the order code symbol "e" or "E", and the order code (can only be integers, can be signed). Its general form is a E n (a is a decimal number, n is a decimal integer), and its value is a \ * 10. For example, 2.1E5 (equal to 2.1 \ * 10, 5), 3.7E \ -2 (equal to 3.7 \ * 10, -2 \ *), 0.5E7 (equal to 0.5 \ * 10, 7), -2.8E \ -2 (equal to -2.8 \ * 10, -2 *)
The following are not valid real numbers:
345 (no decimal point) E7 (no number before order code symbol E) \ -5 (no order code symbol) 53 \ - E3 (incorrect negative sign position) 2.7E (no order code).
Standard C allows floating-point numbers to use suffixes. The suffix 'f' or 'F' indicates that the number is a floating-point number. 356f and 356 are equivalent.
1.6. Real variable
Real variables are divided into two categories: single precision and double precision, with type descriptors being float single precision and double precision. In Turbo C, the single precision type occupies 4 bytes (32 bits) of memory space, with a numerical range of 3.4E \ -38 to 3.4E+38, and can only provide seven significant digits. The double precision type occupies 8 bytes (64 bits) of memory space, with a numerical range of 1.7E \ -308 to 1.7E+308, and can provide 16 significant digits.
The format and writing rules for the description of real variables are the same as those for integers.
example: float x,y; (x,y are single precision real quantities)
double a,b,c; (a,b,c are double precision real type quantities)
Real constants are not classified as single or double precision, and are treated as double precision double type.
float a = 32;
float b;
double d;
b = 12345678;
d = b * 100;
d = d + a;
d = d + 58.123456;
1.7. Character type quantity
Character variables include character constants and character variables.
A character constant is a character enclosed in single quotation marks. For example, 'a', 'b', '=', '+', '?' All are legal character constants. In C language, character constants have the following characteristics:
Character constants can only be enclosed in single quotes, not double quotes or other parentheses.
Character constants can only be single characters and cannot be strings.
Characters can be any character in the character set. But once numbers are defined as character types, they cannot participate in numerical operations. 5 'and 5 are different 5 'is a character constant and cannot participate in operations.
1.8. Character variable
The value of a character variable is a character constant, which is a single character. The type descriptor for character variables is char. The format and writing rules for character variable type descriptions are the same as those for integer variables.
Example:
char a,b; Each character variable is allocated one byte of memory space, so only one character can be stored. Character values are stored in the memory units of variables in the form of ASCII code. The decimal ASCII code for x is 120, and the decimal ASCII code for y is 121. Assign 'x' and 'y' values to character variables a and b: a='x '; b='y'; Actually, the binary code of 120 and 121 is stored in the memory of units a and b
a 0 1 1 1 1 0 0 0
b 0 1 1 1 1 0 0 1
So they can also be regarded as integer quantities. C language allows assigning character values to integer variables and also allows assigning integer values to character variables. When outputting, it is allowed to output character variables as integers and integers as characters. The integer amount is two bytes, and the character amount is one byte. When the integer amount is processed according to the character amount, only the lower eight bytes participate in the processing.
char a = 49;
char b;
char d;
b = a + 10;
d = a + b;
char c1, c2;
c1 = 'a';
c2 = 'b';
c1 = c1 - 32;
c2 = c2 - 32;
1.9. String constant
A string constant is a sequence of characters enclosed in a pair of double quotation marks. For example, "CHINA", "C program:", "$12.5", etc. are all valid string constants. String constants and character constants are different quantities. There are mainly the following differences between them:
Character constants are enclosed in single quotes, while string constants are enclosed in double quotes.
Character constants can only be a single character, while string constants can contain one or more characters.
A character constant can be assigned to a character variable, but a string constant cannot be assigned to a character variable. There is no corresponding string variable in C language.
Character constants occupy one byte of memory space. The number of memory bytes occupied by string constants is equal to the number of bytes in the string plus 1. The added byte stores the character '\ 0' (ASCII code 0). This is the symbol of the end of the string. For example, the byte size of the string "C program" in memory is: C program \ 0. Although both the character constant 'a' and the string constant 'a' have only one character, their situations in memory are different.
'a' occupies one byte in memory and can be represented as: a
'a' occupies two bytes in memory and can be represented as: a \ 0 signed constant
1.10. Symbolic constant
In C language, a constant can be represented by an identifier, which is called a symbolic constant. Symbolic constants must be defined before use, and their general form is:
#define identifier constant
Among them, # define is also a preprocessing command called the macro definition command, whose function is to define the identifier as the constant value after it. Once defined, all occurrences of this identifier in the program will be replaced by the constant value. Traditionally, constant identifiers are written in uppercase letters, while variable identifiers are written in lowercase letters to distinguish them.
#define PI 3.14159
void main()
{
float s, r;
r = 5;
s = PI * r * r;
printf("s=%f\n", s);
}
Define the PI as 3.14159 s and r as the real number 5 \ ->r PI \ * r \ * r \ ->s using the macro defined command
Display program results float s, r; r=5; s=PI*r*r; This program defines the PI as 3.14159 by the macro definition command before the main function, and replaces the PI with this value in the program. S=PI \ * r \ * r is equivalent to s=3.14159 \ * r \ * r. It should be noted that symbolic constants are not variables, and the value they represent cannot be changed throughout the entire scope. That is to say, in the program, it cannot be reassigned using assignment statements.