Word Source VC World - C language classroom
Self-archiving, also dedicated to the same Delphi programmer as me
C language data type
The description of the variable can include three aspects: • Data type · Storage type · Scope so-called data type is in the desired nature, representation, occupying the amount of storage space, and constructive characteristics. In C language, the data type can be divided into: basic data type, constructive data type, pointer type, and four categories.
Basic data type
The basic data type is the most important feature that its value can no longer decompose into other types. That is, the basic data type is self-description.
2. Construct data type construction data type
It is defined based on the method of constructing according to the defined one or more data types. That is, the value of a structure type can be broken down into several "members" or "elements". Each "member" is a basic data type or is another constructor. In C language, the structure type has the following: • Array type · Structural type · Joint type
3. Pointer type
The pointer is a special, and it is also a data type with important role. Its value is used to indicate an address in the memory in the memory. Although the value of the pointer variable is similar to the intensive amount, this is the two types of completely different amounts, so it cannot be mixed. 4. Empty Type It Type The specifier is VOID.
Constants and variables
Integer
The intensity includes integral constants, integer variables. Integral constants is an integral. In C language, the complete number of use has three kinds of eight-in-one, hexadecimal and decimal.
Integrity constant
1. Octa-made constant eight-feed immologists must begin with 0, that is, 0 as a prefix of the eight-input number. The digital value is 0 to 7. The number of octaves is usually unsigned. The following numbers are legal octaves: 015 (decimal 13) 0101 (decimal 65) 0177777 (decimal 65535) The following number is not legal octal number: 256 (unprogin 0) 03A2 (including non-eight-way digital digital) -0127 (there is a negative)
2. The prefix of the hexadecimal rectification constant is 0x or 0x. Its digital value is 0 ~ 9, A ~ f or A ~ F. The following numbers are legally hexadecimal impromptus: 0x2a (decimal 42) 0xA0 (decimal 160) 0xFFF (decimal 65535) The following number is not a legal hexadecimal general number: 5A (unproped 0x) 0x3h (containing non-heteo digital)
3. Decimomed all dozens into the formation constant without prefix. Its digital is 0 ~ 9. The following numbers are legal decimers: 237 -568 65535 1627 The following number is not a legal decimal general number: 023 (can not have a preamble 0) 23D (including non-ten credit Digital)
In the program, it is distinguished according to the prefix. Therefore, don't make the prefix correctly when writing constants is incorrect. 4. The suffix of the integer constant is on the 16-bit word long, the basic integer is also 16 bits, so the scope of the number indicated is also limited. The decimal number of endless symbols is from 0 to 65535, and the number of symbols is -32768 ~ 32767. The representation of the octal unsigned number is 0 ~ 0177777. The hexadecimal non-symbolic number of representations is 0x0 ~ 0xFFFF or 0x0 ~ 0xFFFF. If the number of uses exceeds the above range, it must be represented by long integer. Long integer is represented by suffix "L" or "L". For example: decimal length is 158L (decimal 158) 358000L (decimal is -358000) octal long improvement 012L (decimal 10) 077L (decimal 63) 0200000L (decimal 65536) hexadecimal length often 0x15L ( Teconation 21) 0xA5L (decimal 165) 0x10000L (decimal 65536) Long integer 158L and basic completion 158 There is no difference in value. But to 158L, because the C-compilation system will allocate 4 byte storage space for it. For 158, since it is basically integrated, only 2 bytes of storage space is allocated. Therefore, pay attention to the operation and output format, avoid errors. No symbol numbers can also be represented by the suffix, the suffix of unsigned number of integer constants is "U" or "U". For example: 358U, 0x38au, 235lu are both unsigned. Prefix, suffix can be used simultaneously to represent various types of numbers. If 0xA5lu means hexadecimal unsigned long integer A5, its decimal is 165. Integer variable
Integer variables can be divided into the following categories: 1. The basic type specifier is int, which is 2 bytes in memory, and the value is fundamental. 2. Short INT or SHORT 'C110F1 in the shortity type specifier. All bytes and value ranges are the same as the basic type. 3. Long integer type specifier is long int or long, accounting for 4 bytes in memory, and the value is long and constant. 4. No symbol type Type Description is unsigned. The unsigned type can also be composed of three types of these types: (1) Signature Basic Type Description is unsigned int or unsigned. (2) No symbolic short integer type specifier for unsigned short (3) No symbolic long integer type specifier is the same amount of memory spatial bytes of the number of unsigned types of all kinds of non-symbolic types. . But because the symbol bit is saved, it cannot be represented. The following table lists the representations of the number of memory by allocation of various types in Turbo C. Type Description Number INT -32768 ~ 32767 ■■ Short Int -32768 ~ 32767 ■ ■ ■ ■■■■■■■■■■ UNSIGNED INT 0 ~ 65535 ■ ■ ■ ■ ■■■ Unsigned long 0 ~ 4294967295 ■■■■ Description Variables for integer variables Description of variables Description: Type Description Variable Name identifier, variable name identifier, ...;, for example: int A, B, C; (A, B, C is integer variable) long x, y; (x, y is long integer variable) Unsigned P, q; (p, q is unsigned integer variable)
When writing variables, you should pay attention to the following points: 1. Allows that multiple of the same types of variables are allowed after a type specifier. Between the variable names are separated by commas. Type indicators are at least one space interval between the variable name. 2. The last variable name must be ended with ";". 3. Variable description must be placed before the variable is used. Generally placed at the beginning of the function body.
Real capacity
Real constant
The real shape is also called floating point. Real aspects are also referred to as real or floating point numbers. In the C language, the real numbers only use decimal. It has two forms: decimal form index form 1. Decimal number consists of 0 to 9 and decimal points. For example: 0.0, .25, 5.789, 0.13, 5.0, 300., - 267.8230, etc. are all legal realities. 2. Index form is constructed by decimal number, plus group code flag "e" or "e", and class code (only an integer, can be symbol). Its general form is A E n (a is a decimal number, n is a decimal integer) its value is A * 10, N as: 2.1e5 (equal to 2.7 * 10), 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 is not a legal real number 345 (no small number point) E7 (untaled before the code flag e Numbers) -5 (no marked sign) 53.-E3 (negative position is not) 2.7e (no line) Standard C allows floating point numbers to use suffixes. The suffix is "f" or "f" means that the number is the floating point number. Such as 356f and 356. is 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 to end real variables using Printf display results
The real variable is divided into two categories: single precision type and double-precision type, the type of type is a Float single precision specifier, Double double precision specifier. Single precision type in Turbo C accounts for 4 bytes (32-bit) memory space, and its numerical range is from 3.4e-38 ~ 3.4e 38, only seven valid numbers can only be provided. Double-precision type accounts for 8 bytes (64-bit) memory space, and its numerical range is 1.7e-308 ~ 1.7e 308, which provides 16-bit valid numbers. The format and writing rules illustrated by real variables are identical. For example: float x, y; (x, y is single precision real) Double A, B, C; (A, B, C are double precision real amount) real constant is not divided, double precision, Double precision Double processing.
Character amount
The character model includes character constants and character variables.
Character constant characters constant is a character enclosed in single quotes. For example, 'a', 'b', '=', ' ', '?' Is all legitimate character constants. In C language, character constants have the following characteristics: 1. Character constants can only be enclosed in single quotes, and cannot be used in double quotes or other parentheses. 2. Character constants can only be single characters and cannot be a string. 3. The character can be any character in the character set. But numbers cannot participate in numerical operations after they are defined as characters. For example, '5' and 5 are different. '5' is a character constant and cannot participate in the operation.
Essential character escape characters is a special character constant. The escape character begins with a backslash "/", followed by one or a few characters. The escape character has a specific meaning, different from the original meaning of characters, so that "escape" characters. For example, "/ n" used in the format string of the previous example Printf function is a escape character, and its meaning is "Enter the Running Wrap". The escape character is primarily used to indicate those control code that is inconvenient to use in general characters. Common escape character and its meaning of its meaning / emission character escape character / n Enter-in-line / T lateral jump to the next table list / v vertical jumping Ground / B retracted / R Enter / f Pass Page // Antilancing "/" / 'Single Number / A ring / DDD 1 ~ 3 bits of the character / XHH 1 ~ 2 hexadecimal speech representatives represent the characters represented by the number, Any character set in the C language character can be represented by a escape character. The / DDD and / XHH in Table 2.2 is proposed for this purpose. DDD and HH are an octal and hexadecimal ASCII code. Such as / 101 indicates the word "quot; A", / 102 indicates that the letters "B", / 134 indicate the reverse slope, / XOA means a wrap, or the like. The use of the character variable character variable of the escape character is character constant, ie a single Character. The type of character variable is the format and writing rules for the character variable type. The format and writing rules are the same as the integer variable. For example: CHAR A, B; each character variable is assigned a byte memory space, so only Store a character. The character value is stored in the memory cell of the ASCII code. For example, the decimal ASCII code is 120, Y's decimal ASCII code is 121. The character variables A, B give 'x' and 'y' value: a = 'x'; b = 'y'; actually stores 120 and 121 binary code within A, B cells: A 0 1 1 1 0 0 0 B 0 1 1 1 1 0 0 1 So it can also be regarded as an integer. The C language allows the integer variable to assign a character value, which allows the character variable to be integrated. When output, the character variable is allowed to be integrated The amount output, also allows the integer amount to be output by the character quantity. The total amount is two-word output, the character amount is single-word output, when the integer amount is processed by the characterization, only low eight bytes participation processing .
String constant string constant is a character sequence enclosed by a pair of double quotes. For example: "China", "C Program:," $ 12.5 "is all legitimate string constants. String constants and character constants are different. They have the following differences between them: 1. Character constants Get up, string constants are enclosed by double quotes .2. Character constants can only be single characters, string constants can contain one or more characters .3. You can give a character constant to a character variable, but you can't put a character CRRAMS is given a character variable. There is no corresponding string variable in the C language. This is different from the BASIC language. But you can use a character array to store a string constant. In the chapter chapter, it is introduced. 4. Character Constants account for one byte memory space. The number of memory bytes of string constants is equal to the character string. The number of bytes plus 1. Add characters "/ 0" (ASCII code is 0). This is a character The symbol of the string. For example, the character string "C Program" in memory is: c program / 0. Character constant 'a' and character string "a" although there is only one character, but in memory The situation is different. 'A' takes one byte in memory, which can be represented as: a "a" accounts for two bytes in memory, which can be represented as: A / 0 symbol constant symbol constant in C language, You can use an identifier to represent a constant called symbol constant. The symbol constant must be defined before use, and its general form is: #define identifier constant where #define is also a pretreatment command (preprocessing command? Quot ; # "Starting), called a macro definition command (will be further described in Chapter 9 Polyprocessor), which is the function to define the identifier as the subsequent constant value. Once defined, there will be this constant value in all the places where the identifier in the program will be in the program. The identifier of the symbol constant is used in uppercase letters, and the variable identifier is small-sized by lowercase letters, in order to distinguish. #define pi 3.14159
The initial value and type conversion of the variable
Variables The first value often needs to be assigned to variables in the program to use variables. There are a variety of methods in the language program, and this method is called initialization when defined. The general form of the initial value in the variable description is: Type Description Variable 1 = Value 1, Variable 2 = Value 2, ...;, for example: int a = b = c = 5; float x = 3.2, y = 3f, Z = 0.75; CHAR CH1 = 'k', CH2 = 'p'; should be noted that continuous assignment is not allowed in the description, such as A = B = C = 5 is illegal.
The data type of the conversion variable of the variable type can be converted. There are two ways to conversion, one is an automatic conversion, one is forced conversion.
Automatic conversion automatic conversion occurs when mixing the amount of different data types, is automatically completed by the compilation system. Automatic conversion follows the following rules: 1. If the type of computation is different, first convert to the same type, then operate. 2. Conversion is performed in a direction in which the data length is increased to ensure that the accuracy does not decrease. For example, when an int type and a long type, the int quantity is then converted to the long type. 3. All floating point operations are carried out in double precision, even if only the expression of the Float single-intensity metrics, you must first turn into a Double type, and then operate. 4. CHAR type and short type participate in the operation, it must be converted to an INT type. 5. In assignment, the data type of the assignment number is not at the same time, the type of the value of the assignment number will be converted to the type of the left. If the left length of the data type length is long, a part of the data will be lost, which will reduce the accuracy, and the lost part is rounded forward. Figure 21 shows the rules for type automatic conversion. Void main () {float pi = 3.14159; INT S, R = 5; s = r * r * pi; printf ("s =% d / n", s);} Pi <- 3.14159s <- 0 , R <- 5S <- r * r * pi display procedure run results float pi = 3.14159; INT S, R = 5; s = r * r * pi; in this case, PI is a real shape; s, r is integer. R and Pi are converted to Double type calculations when performing S = R * R * PI statement, and the result is also Double type. However, since S is integer, the result is still intellectual, and the fractional part is went. Forced type conversion mandatory type conversion is achieved by a type conversion operation. Its general form is: (Type Description) (Expression) The function is to convert the calculation result of the expression to the type represented by the type of description. For example: (float) A converts A converted to realistic (int) (x y) to convert the results of X Y to integer when using forced conversion: 1. Type indicators and expressions must Add parentheses (single variables may not be bordered), such as writing (IN Y) (int) x y, it is added to Y after converting X and Y. 2. Whether it is forced conversion or auto-conversion, it is only a temporary conversion of the data length of the variable for the need for this operation, without changing the type of variable. Main () {float f = 5.75; Printf ("(int) f =% D, f =% f / n", (int) f, f);} f <- 5.75 convert FLOAT F to INT F Float f = 5.75; Printf ("" f =% D, f =% f / n ", (int) f, f); this example shows that although f is forced to convert to int type, only in operation The role is temporary, and the type of F itself does not change. Therefore, the value of (int) f is 5 (deleted decimal) and the value of F is still 5.75.
Basic operators and expressions
Most of the operator and the number of operators and expressions in the species, priority and binding C language, are rare in advanced languages. It is rich operators and expressions to make C language function is very perfect. This is also one of the main features of the C language. The C language operator does not only have different priorities, but also a feature is its combination. In the expression, the order of operations participating in the calculation is not only necessary to comply with the operator priority, but also restrictively restrict the integration of the operator to determine whether it is from left to right or to the left. This binding is not there, thereby also increased the complexity of C language in other advanced languages.
The operator's type C language operator can be divided into the following categories: 1. Arithmetic operator is used for all kinds of numerical operations. Including plus ( ), minus (-), multiplied (*), except (/), resufficient (or modeling operation,%), self-increasing ( ), self-reduced (-) a total of seven. 2. The relationship operator is used to compare the operation. Including greater than (>), less than (<), equal to (==), is greater than or equal to (<=), and is equal to six in (! =). 3. Logic operators are used for logical operations. Includes three kinds of (&&), or (||), not (!). 4. The bit operation operator is involved in the amount of calculation, and according to the binary position. Including bit and (&), bit or (|), bit is not (~), bit or (^), left shift (<<), and right (>>). 5. Assignment operator is used to assign value, divided into simple assignment (=), composite arithmetic assignment ( =, - =, * =, / =,% =) and composite operation assignment (& =, | =, ^ =, >> =, << =) The three types of total is 11. 6. Conditional operator This is a three-mean operator for condition evaluation (? :). 7. The comma operator is used to combine several expressions into an expression (,). 8. The pointer operator is used to take content (*) and the address (&) two operations. 9. Ask bytes to calculate the number of bytes accounted for data types (SIZEOF). 10. Special operators include bracket (), subscript [], member (→ ,.) and other kinds. In the priority and binding C language, the operator's operation priority is divided into 15 levels. Level 1, the lowest level 15. In the expression, the priority is higher than the lower priority. When the operator priority on both sides of an operation is the same, the bonding direction specified by the combination of the operator is treated. The combination of each operator in the C language is divided into two, ie left bonding (from left to right) and right binding (from right to left). For example, the combination of the arithmetic operator is from left to right, that is, the left and right. If there is an expression X-Y Z, Y should be combined with "-", execute the X-Y operation, and then execute the z. This binding direction from left to right is called "left binding". The binding direction from right to left is called "right binding". The most typical right binding operator is an assignment operator. Such as x = y = z, due to the right binding of "=", Y = Z is executed first, and the X = (y = z) operation is performed. There are many of the C language operators for the right binding, and they should be considered to avoid understanding errors.
Arithmetic operators and arithmetic expressions Basic arithmetic operators 1. The addition operator " " addition operator is a binocular operator, that is, there should be two quantities to participate in the addition. Such as A B, 4 8, etc. Have right binding. 2. The subtraction operator "-" The subtract operator is a binocular operator. However, "-" can also be used as a negative operator, at this time, the single-grade operation, such as -X, -5, and the like have left bonding. 3. Multiplication operator "*" binocular operation with left conjunction. 4. Separate operators "/" binding have left bonding. When the amount of participation is integrated, the results are also intellectual, and the decimal is went. If there is a real shape in the calculation, the result is a double precision. Void main () {Printf ("/ N / N% D,% D / N", 20/7, -20 / 7); Printf ("% f,% f / n", 20.0 / 7, -20.0 / 7);} The binocular operation has left bonding. When the amount of participation is integrated, the results are also intellectual, and the decimal is went. If there is a real shape in the calculation, the result is a double precision. Printf ("/ N / N% D,% D / N", 20/7, -20 / 7); Printf ("% f,% f / n", 20.0 / 7, -20.0 / 7); this example The results of 20/7, -20 / 7 are all integrated, and the decimal is all right. 20.0 / 7 and -20.0 / 7 is also a real shape due to a real number of participation. 5. Exercise the operator (the model operator) "%" binocular operation with left bonding. The amount involved in the participation is integrated. The result of the remaining operation is equal to the remainder of the two times. Void main () {Printf ("% d / n", 100% 3);} Both the binoculant, with left conjunction. The expense operator is required to participate in the amount of operations. This example output 100 divides the remaining number 1 obtained by 3.
Self-increasing 1, the self-reduction 1 operator self-increment 1 operator is " ", and its function is to increase the value of the variable 1. The self-reduction 1 operator is "-", and its function is to make the variable value from 1. Self-increasing 1, the self-reduction 1 operator is single-grade operation, all have a right binding. There are several forms: i i is added to other calculations after it is incremented. - I then participate in other calculations after the reduction of 1. When I i is involved in the operation, the value of i is again increased. I - i After the operation, the value of i is reduced by 1. It is prone to i and i-i-in understanding and use. Especially when they are in a more complex expression or statement, it is often difficult to clarify and should be carefully analyzed. Void main () {INT i = 8; Printf ("% d / n", i); Printf ("% d / n", - i); Printf ("% d / n", i ); Printf ("% d / n", i -); printf ("% d / n", - i ); Printf ("% d / n", - i - i -);} i <- 8i <- -i 1i <- i-1i <- i 1i <- i-1 I = 8; Printf ("% D / N", I); Printf ("% d / n", - i); Printf ("% d / n", i ); Printf ("% d / n", i -); printf ("% d / n ", -i ); Printf ("% d / n ", - i - I -); i's initial value of 8 second line i plus 1 post-output is 9; the third line minus 1, the output is 8; The fourth line output i is 8 after 8, then add 1 (9); the 5th line output i is 9 after 9, then reduce 1 (8); the sixth line output -8, add 1 (9); After output -9, then 1 (8) void main () {INT i = 5, J = 5, P, q; P = (i ) (i ) (i ); Q = ( J) ( J) ( J); Printf ("% D,% D,% D,% D", P, Q, I, J);} i <- 5, j <- 5 , P <- 0, q <- 0i i i ---> p, i 1 -> i, i 1 -> i, i 1 -> ij 1-> j , J 1-> J, J 1-> J, J J J-> Q INT I = 5, J = 5, P, Q; P = (i ) (i ) (i ); q = ( j) ( j) ( j); in this program, P = (i ) (i ) (i ) should be understood as three I added, so P value Take 15. Then I will increase from 1 three times equivalent to the final value of I. For Q, q = ( J) ( J) ( J) should be understood as q First from 1, then participate in the operation, because Q Self-increased 1 three times after the value of 8 The three 8 additions and the last value of J is still 8.
The arithmetic expression expression is a form that is combined by constants, variables, functions, and operators. An expression has a value and its type, which is equal to the values and types of the results obtained by the expression. The expression evaluation is performed in the order of the priority of the operator and the conjunction. A single constant, variable, and function can be considered as a special example of expression. The arithmetic expression is a form of an arithmetic operator and a parentheses. The following is an example of an arithmetic expression: A B (A * 2) / C (x r) * 8- (A B) / 7 I sin (x) sin (y) ( i) - (j ) (k -)
Assignment operators and assignment expressions simple assignment operators and expressions, simple assignment operations are "=". A child connected by "=" is called an assignment expression. Its general form is: variable = expression, for example: x = a bw = sin (a) sin (b) y = i j assignment function is to calculate the value of the expression to give the left variable. The assignment operator has a right binding. Therefore, A = B = c = 5 can be understood as a = (b = 5)) In other advanced languages, the assignment constitutes a statement called an assignment statement. In C, "=" is defined as an operator, thereby constituting an assignment expression. An inviginable expression can occur if an expression can appear. For example, the bonus x = (a = 5) (b = 8) is legal. Its meaning is to give 5 A, 8 to B, then add A, B, and impart X, so X should be equal to 13. In the C language, an assignment statement can also be configured, and according to the C language, any expression is configured as a statement in its unscissable semicolon. Therefore, as X = 8; a = b = c = 5; all assignment statements, we have used a lot in the previous examples. If the data type on both sides of the assignment is different, the system will automatically perform type conversion, that is, the type of assignment is replaced with the type of the left. Specifically, as follows: 1. The real shape gives an integer, went to the fractional portion. The first example 2.9 has explained this. 2. Integer to the real shape, the value is constant, but will be stored in floating point, that is, increase the fractional portion (the value of the fractional portion is 0). 3. Character type gives an integer, because the character type is one byte, and the integer is two bytes, so the character's ASCII code value is placed in the low output, high eight digits 0.4 The integer gives a character type, only gives the low eight digits to the character. Void main () {Int a, b = 322; float x, y = 8.88; CHAR C1 = 'k', C2; A = Y; X = B; a = C1; C2 = B; Printf ("% D, % F,% D,% C ", A, X, A, C2);} int A, b = 322; float x, y = 8.88; CHAR C1 = 'k', C2; Printf ("% D,% f,% D,% C ", A = Y, X = B, A = C1, C2 = b); this example indicates the rules of the type conversion in the above assignment operation. A is integer, give the real amount Y value 888, only the integer 8. X is a real shape, impart an integer B value 322, and then adds the fractional portion. The character amount C1 is given to a change, and the integer B is given to C2 to take the low eight bits to become a character type (the low eight bits of B is 01000010, i.e., decimal 66, press the ASCII code to correspond to character B).
The composite assignment and expression can form a compound assignment before the assignment "=". Such as =, - =, * =, / =,% =, << =, >> =, & =, ^ =, | =. The general form of composite assignment expressions is: variable binocular operator = expression It is equivalent to variable = variable operator expression, for example: A = 5 equivalent to A = A 5 x * = Y 7 equivalent In x = x * (y 7) R% = P is equivalent to R = R% P composite assignment, this way of writing, the beginner may not habred, but it is very beneficial to compile processing, can improve compilation efficiency and quality Higher target code. The comma operator and comma expression in comma operator C language comma "" is also an operator called a comma operator. Its function is to connect two expressions into an expression called a comma expression. Its general form is: Expression 1, the expression 2 is the value of the two expressions, respectively, and as the value of the expression 2 as the entire comma expression. Void main () {INT A = 2, B = 4, C = 6, x, y; y = (x = a b), (B C); Printf ("Y =% D, x =% D ", y, x);} a <- 2, b <- 4, c <- 6, x <- 0, y <- 0x <- a b, y <--- B In this example, Y is equal to the value of the entire comma expression, that is, the value of the expression 2, X is the value of the first expression. Two points in the comma expression also explain: 1. Expression 1 and Expression 2 in general forms of comma expressions can also be a comma expression. For example: Expression 1, (Expression 2, Expression 3) forms a nested situation. Therefore, the comma expression can be extended to the following form: Expression 1, Expression 2, ... Expression N The value of the entire comma expression is equal to the value of the expression N. 2. Using a comma expression in the program, usually, the value of each expression in which the comma expression is required, and the value of the entire comma expression is not necessarily required. 3. Not a comma expression in all comma, as in variable descriptions, the commas in the function parameter table is only used as a space between each variable.