Basic data types include: integer, real, character, pointer, etc.
Extended data types include: array, structure, union, ***, etc.
You can also have custom types.
The following is a detailed description of the basic types
It is best to read a C language book, either paper version or electronic version. It is recommended to read Tan Gaoqiang's c programming, the latest is the third edition, and I was studying it in the second edition
Integer quantities
Integer quantities include integer constants and integer variables. An integer constant is an integer constant. In C language, there are three types of integer constants used: octal, hexadecimal and decimal.
Integer constants
1. Octal integer constants Octal integer constants must start with 0, that is, use 0 as the prefix of the octal number. The digital value is 0~7. Octal numbers are usually unsigned.
The following numbers are legal octal numbers:
015 (13 in decimal) 0101 (65 in decimal) 0177777 (65535 in decimal)
The following The number is not a legal octal number:
256 (no prefix 0) 03A2 (contains non-octal digits) -0127 (minus sign appears)
2. Hexadecimal integer Constants
Hexadecimal integer constants are prefixed with 0X or 0x. Its digital value is 0~9, A~F or a~f.
The following numbers are legal hexadecimal integer constants:
0X2A (decimal is 42) 0XA0 (decimal is 160) 0XFFFF (decimal is 65535)
< p>The following numbers are not legal hexadecimal integer constants:5A (no prefix 0X) 0X3H (contains non-hexadecimal digits)
3. Decimal integer constants
Decimal integer constants have no prefix. Its numbers range from 0 to 9.
The following numbers are legal decimal integer constants:
237 -568 65535 1627
The following numbers are not legal decimal integer constants:
023 (cannot have leading 0) 23D (contains non-decimal digits)
In the program, various base numbers are distinguished based on prefixes. Therefore, when writing constants, do not use the wrong prefix to cause incorrect results. 4. The suffix of the integer constant is on a machine with a 16-bit word length. The length of the basic integer is also 16 bits, so the range of the number represented is also limited. The range of decimal unsigned integer constants is 0~65535, and the signed numbers are -32768~+32767. The representation range of octal unsigned numbers is 0~0177777. The representation range of hexadecimal unsigned numbers is 0X0~0XFFFF or 0x0~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 long integer constant 158L (decimal is 158) 358000L (decimal is -358000)
Octal long integer constant 012L (decimal is 10) 077L (decimal is 63) 0200000L (Decimal is 65536)
Hexadecimal long integer constant 0X15L (Decimal is 21) 0XA5L (Decimal is 165) 0X10000L (Decimal is 65536)
Long integer 158L and basic integer The constant 158 ??makes no numerical difference. But for 158L, because it is a long integer, the C compilation system will allocate 4 bytes of storage space for it. For 158, because it is a basic integer type, only 2 bytes of storage space are allocated. Therefore, attention should be paid to the operation and output format to avoid errors. Unsigned numbers can also be represented by suffixes. The suffix for unsigned numbers of integer constants is "U" or "u". For example: 358u, 0x38Au, 235Lu are all unsigned numbers. Prefixes and suffixes can be used at the same time to represent various types of numbers. For example, 0XA5Lu represents the hexadecimal unsigned long integer A5, which is 165 in decimal.
Integer variables
Integer variables can be divided into the following categories:
1. Basic type
The type specifier is int , occupying 2 bytes in memory, and its value is a basic integer constant.
2. Short integer
The type specifier is short int or short'C110F1. The occupied bytes and value range are the same as those of the basic type.
3. Long integer
The type specifier is long int or long, which occupies 4 bytes in memory, and its value is a long integer constant.
4. Unsigned type
The type specifier is unsigned.
The unsigned type can be formed by matching the above three types:
(1) Unsigned basic type The type specifier is unsigned int or unsigned.
(2) The unsigned short integer type specifier is unsigned short
(3) The unsigned long integer type specifier is unsigned long
Each An unsigned type occupies the same number of bytes of memory space as the corresponding signed type. However, since the sign bit is omitted, negative numbers cannot be represented. The following table lists the number of memory bytes allocated for various integer types in Turbo C and the representation range of the number.
The number of bytes allocated in the range of the type specifier number
int -32768~32767 ■■
short int -32768~32767 ■■
signed int -32768~32767 ■■
unsigned int 0~65535 ■■
long int -2147483648~2147483647 ■■■■
unsigned long 0~4294967295 ■■■■
Description of integer variables
The general form of variable description is: type specifier 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, you should pay attention to the following points:
1 .Allows multiple variables of the same type to be specified after a type specifier. Separate variable names with commas. There should be at least one space between the type specifier and the variable name.
2. The last variable name must end with ";".
3. The variable description must be placed before the variable is used. Usually placed at the beginning of the function body.
[Practice] //1int a,b;
short int c;
short d=100;
a=d -20;
b=a+d;
c=a+b+d;
d=d-a+c-b;'Vtable p>
a,2,0
b,2,0
c,2,0
d,2,100
of Vtable
'Vupdate
1,0;2,0
3,0
4,100
1,80
2,180
3,360
4,200
of Vupdate
of Practice
[Practice] //2int a=5;
int b=9;
long int c;
long d;
c=a+b-7;
d=a*b*c;
c=d*d*d;
a= c-d;'Vtable
a,2,5
b,2,9
c,4,0
d,4 ,0
of Vtable
'Vupdate
1,5
2,9
3,0
4,0
3,7
4,315
3,31255875
1,-5112< /p>
of Vupdate
of Practice
[Practice] //3int a=6,b=19;
unsigned int c;< /p>
int d;
c=a-b+7;
d=b*c;
a=b+c+ d;
b=-a;'Vtable
a,2,6
b,2,19
c,2 ,0
d,2,0
of Vtable
'Vupdate
1,6;2,19
3,0
4,0
3,65530
4,-114
1,-101
p>2,101
of Vupdate
of Practice
void main(){
long x,y;
p>int a,b,c,d;
x=5;
y=6;
a=7;
b=8;
c=x+a;
d=y+b;
printf("c=x+a= %d,d=y+b=%d\n",c,d);
}
Describe main as returning void, that is, not returning any type of value< /p>
x, y are defined as long type
a, b, c, d are defined as int type
5->x
6->y
7->a
8->b
x+a->c
y+b ->d
Display the program running result of long x,y;
int a,b,c,d;
c=x+a;< /p>
d=y+b;
You can see from the program: x, y are long integer variables, a, b are basic integer variables. Operations are allowed between them, and the operation results are long integer types. However, c and d are defined as basic integer types, so the final result is a basic integer type. This example illustrates that quantities of different types can participate in operations and assign values ??to each other. The type conversion is automatically completed by the compilation system. The rules for type conversion will be introduced later.
Real type quantity
Real type constant
Real type is also called floating point type. Real constants are also called real numbers or floating point numbers. In C language, real numbers only use decimal notation. It has two forms: decimal number form exponential form
1. Decimal number form
It consists of the digits 0~9 and the decimal point. For example: 0.0, .25, 5.789, 0.13, 5.0, 300., -267.8230, etc. are all legal real numbers.
2. Exponential form
It consists of decimal number, plus exponent code sign "e" or "E" and exponent code (can only be an integer, 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,n such as: 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 legal real numbers 345 (no decimal point) E7 (nothing before the exponent sign E Number) -5 (No exponent flag) 53.-E3 (Negative sign in wrong position) 2.7E (No exponent)
Standard C allows floating-point numbers to use suffixes. A suffix of "f" or "F" indicates that the number is a floating point number. For example, 356f and 356. are equivalent. Example 2.2 illustrates this situation:
void main()
{
printf("%f\n%f\n",356., 356f);
}
void indicates that main does not return any value and uses printf to display the result.
Real type variable
Real type variable Divided into two categories: single precision type and double precision type.
The type specifier is float single precision specifier and double double precision specifier. In Turbo C, the single precision type occupies 4 bytes (32 bits) of memory space, its value range is 3.4E-38~3.4E+38, and can only provide seven significant digits. The double precision type occupies 8 bytes (64 bits) of memory space, its value range is 1.7E-308~1.7E+308, and can provide 16 significant digits.
The format and writing rules of real type variable description are the same as those of integer type.
For example: float x,y; (x,y are single-precision real quantities)
double a,b,c; (a,b,c are double-precision real quantities) Quantity)
Real type constants are not divided into single and double precision, and are all treated as double precision.
void main()
{
float a;
double b;
a=33333.33333;
b=33333.33333333333333;
printf("%f\n%f\n",a,b);
}
This program explains the difference between float and double
a ■■■■
b ■■■■■■■■
a<---33333.33333
b<---33333.33333333333;;
Display program results
This program explains the difference between float and double
float a;
double b;
a=33333.33333;
b=33333.33333333333333; As can be seen from this example, since a is a single-precision floating point type, the effective bit The number is only seven digits. The integer already occupies five digits, so all the numbers after the two decimal places are invalid. b is a double precision type with sixteen valid digits. However, Turbo C stipulates that up to six decimal places should be retained, and the remaining parts should be rounded off.
[Practice] //floatint a=32;
float b;
double d;
b=12345678;
float b; p>
d=b*100;
d=d+a;
d=d+58.123456;'Vtable
a,2, 32
b,4,0.0
d,8,0.0
of Vtable
'Vupdate
1,32
2,0
3,0
2,12345678.00000
3,1234567800
3,1234567832
3,1234567890.123456
of Vupdate
of Practice
[Practice] //1int a=543; p>
float b;
b=123.123962+a;
b=b-100;
a=b;'Vtable
a,2,543
b,4,0.0
of Vtable
'Vupdate
1,543
< p>2,0.02,123.123962
2,23.123962
1,23
of Vupdate
of Practice
Character variables
Character variables include character constants and character variables.
Character constant
A character constant is a character enclosed in single quotes. For example, 'a', 'b', '=', '+', '?' are all legal character constants. In C language, character constants have the following characteristics:
1. Character constants can only be enclosed in single quotes, not double quotes or other brackets.
2. Character constants can only be single characters, not strings.
3. The character can be any character in the character set. But after a number is defined as a character type, it
cannot participate in numerical operations. For example, '5' and 5 are different. '5' is a character constant and cannot participate in operations.
Escape character
The escape character is a special character constant. The escape character starts with a backslash "\" and is followed by one or more characters. Escape characters have specific meanings that are different from the original meaning of the characters, so they are called "escape" characters. For example, the "\n" used in the format string of the printf function in the previous examples is an escape character, and its meaning is "carriage return and line feed". Escape characters are mainly used to represent control codes that are difficult to express using ordinary characters.
Commonly used escape characters and their meanings
The meaning of escape characters
\n Carriage return and line feed
\ t Jump horizontally to the next tab position
\v Skip vertically
\b Backspace
\r Enter
\f Paper feed
\\ Backslash character "\"
\' Single quote character
\a Ringing
\ddd Characters represented by 1 to 3 octal numbers
\xhh Characters represented by 1 to 2 hexadecimal numbers
Broadly speaking, C language Any character in the character set can be represented by an escape character. \ddd and \xhh in Table 2.2 are proposed for this purpose. ddd and hh are the octal and hexadecimal ASCII codes respectively. For example, \101 represents the character "A", \102 represents the letter "B", \134 represents a backslash, \XOA represents a newline, etc.
Use of escape characters
void main()
{
int a,b,c;
a=5; b =6; c=7;
printf("%d\n\t%d %d\n %d %d\t\b%d\n",a,b,c,a ,b,c);
}
This program practices the use of escape characters
a, b, c are integers 5->a,6- >b,7->c
Call printf to display the program running results
printf("%d\n\t%d %d\n %d %d\t\b %d\n",a,b,c,a,b,c);
After the program outputs the a value 5 in the first column, it is "\n", so enter and line feed; then it is "\t", then jump to the next tab position (set the tab position interval to 8), and then output the b value 6; leave two spaces and then output the c value 7, followed by "\n", so enter and line feed. ; After emptying two spaces, the value of a is output, 5; after emptying three spaces, the value of b is output, 6; again, "\t" jumps to the next tab position (aligned with 6 on the previous line), but the next escape The character "\b" returns one space, so the c value 7 is output next to 6.
Character variable
The value of a character variable is a character constant, that is, a single character. The type specifier for character variables is char. The format and writing rules of character variable type descriptions are the same as those of integer variables.
For example:
char a,b; Each character variable is allocated one byte of memory space, so it can only store one character. The character value is stored in the memory unit of the variable in the form of ASCII code. For example, the decimal ASCII code of x is 120, and the decimal ASCII code of y is 121. Assign 'x' and 'y' values ??to character variables a and b: a='x';b='y'; In fact, the binary codes of 120 and 121 are stored in the two 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. During output, character variables are allowed to be output as integers, and integers are also allowed to be output as characters. The integer value is a two-byte value, and the character value is a single-byte value. When the integer value is processed as a character value, only the lower octets participate in the processing.
main()
{
char a,b;
a=120;
b= 121;
printf("%c,%c\n%d,%d\n",a,b,a,b);
}
< p>a ■ b ■a <-- 120
b <--- 121
Display program results
This program It is stated in that a and b are character types, but integer values ??are assigned in the assignment statement. From the results, the output form of a and b values ??depends on the format character in the printf function format string. When the format character is "c", the corresponding output variable value is a character. When the format character is "d", the corresponding output value is The variable value is an integer.
void main()
{
char a,b;
a='x';
b='y';
a=a-32;
b=b-32;
printf("%c,%c\n %d,%d\n",a,b,a,b);
}
a, b are declared as character variables and assigned character values
< p>Convert lowercase letters to uppercase lettersOutput in integer and character types
In this example, a and b are declared as character variables and assigned character values. C language allows Character variables participate in numerical operations, that is, the ASCII codes of characters are used in operations. Since the ASCII codes of uppercase and lowercase letters differ by 32, the lowercase letters are converted into uppercase letters after operation. Then output them in integer and character types respectively.
[Practice] //charint a=49;
char b;
char d;
b=a+10;
d=a+b;'Vtable
a,2,49
b,1,random
d,1, Random
of Vtable
'Vupdate
1,49
2,random
3,random< /p>
2,';'
3,'l'
of Vupdate
of Practice
[Practice ] //char c1,c2;
c1='a';c2='b';
c1=c1-32;c2=c2-32;'Vtable p>
c1,1,random
c2,1,random
of Vtable
'Vupdate
1,random ;2,random
1,'a';2,'b'
1,'A';2,'B'
of Vupdate< /p>
of Practice
String constants
A string constant is a sequence of characters enclosed by a pair of double quotes. For example: "CHINA", "C program:", "$12.5", etc. are all legal string constants. String constants and character constants are different quantities. The main differences between them are as follows:
1. Character constants are enclosed in single quotes, and string constants are enclosed in double quotes.
2. Character constants can only be a single character, while string constants can contain one or more characters.
3. You can assign a character constant to a character variable, but you cannot assign a string constant to a character variable. There is no corresponding string variable in C language.
This is different from the BASIC language. But you can use a character array to store a string constant. It is introduced in the Arrays chapter.
4. Character constants occupy one byte of memory space. The number of memory bytes occupied by a string constant is equal to the number of bytes in the string plus 1. The character "\0" (ASCII code is 0) is stored in the added byte. This is the end of string sign. For example, the bytes occupied by the string "C program" in memory are: C program\0. Although the character constant 'a' and the string constant "a" both have only one character, their situations in memory are different.
'a' occupies one byte in the memory, which can be expressed as: a
"a" occupies two bytes in the memory, which can be expressed as: a\0 symbol Constants
Symbolic constants
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. Its general form is:
#define identifier constant
Where #define is also a preprocessing command (preprocessing commands are all?quot;# "), is called a macro definition command (will be further introduced in Chapter 9 Preprocessor). Its function is to define the identifier as the constant value that follows. Once defined, all subsequent occurrences of the identifier in the program It is customary to use uppercase letters for symbolic constant identifiers and lowercase letters for variable identifiers to indicate the difference. void main()
{
float s,r;
r=5;
s=PI*r*r;
printf("s=%f\n",s);
}
The PI is defined as 3.14159 s by the macro definition command, and r is defined as a real number 5->r PI*r*r->s
Display the program result float s,r; r=5; s=PI*r*r; This program is defined by the macro definition command before the main function PI is 3.14159, and this value is used instead of PI in the program. s=PI*r*r is equivalent to s=3.14159*r*r. It should be noted that the symbolic constant is not a variable, and the value it represents is used throughout the program. The domain cannot be changed. That is to say, it cannot be reassigned using assignment statements in the program.
Copywriting comes from the advertising industry and is the abbreviation of "advertising copywriting", which is translated by the copywriter. Refers to th