Chapter 4 Expression and Assignment
This chapter describes how to construct an expression and assign a value in a C language. Constants, identifiers, strings, and function calls are operated in the expression. The C language has all common language operators. This chapter discusses these operators and the only operator for C or Microsoft. The subject of discussion includes:
* Operations and expressions
* Operator
* Type conversion
Operations and expressions
A "operand" is an entity that operates. A "expression" is an operator and operand sequence that performs any combination of these actions:
* Calculate a value
* Design an object or function
* The operand that generates side effects in c includes constant, identifier, string, function call, subscript expression, member selection expression, and complex expressions formed by operator combination operands or in parentheses, including operands formula. The syntax of these operands is given in the next section "Basic Expression".
Basic expression
The operands in the expression are called "basic expressions". Syntax basic expression:
Identifier
constant
String text
(Expression) expression:
Assignment expression
expression,
Assignment expression
Identifier in the basic expression
The identifier has an integer, FLOAT, ENUM, STRUCT, UNION, arrays, pointers, or functions. An identifier is a basic expression that is illustrated for designing an object (which is a l value) or a function (which is a function indicator). See "L Values and R Value Expressions" for the definition of the l value.
The pointer value represented by an array identifier is not a variable, and therefore, an array identifier cannot form a left operand of an assignment operation. Therefore, it is not a modified L value.
A identifier as a function indicates that a value of the value is the function address. This pointer address points to a function that returns a specified type value. Therefore, the function identifier cannot be a l value in the assignment operation. For more information, see "Identifiers" in Chapter 1 "C".
Constant in basic expressions
A constant operation has the value and type of its given constant value. A character constant has an int type. An integer constant has int, long, unsigned int or unsigned long type, depending on the size and specified value of this integer. See Chapter 1, "Class C" in Chapter 1, "Constant" in Chapter 1.
String text in basic expressions
A "string text" is a character, wide character, or adjacent character sequence enclosed with a dual quotation. Since they are not variables, string or any of their elements cannot be used as an assignment operation. A string text is the array of Char (or width string text Wchar_T array). Convert into a pointer in an expression in an expression. For more information on strings, see Chapter 1, "Basic Elements" of C. ".
Expression in parentheses
You can enclose any operands in parentheses without changing the type or value of the enclosed expression. For example, in the expression:
(10 5) / 5
10 5 in parentheses mean the first value of 10 5, which becomes the left operation of the division (/) operator. (10 5) / 5 results are 3. The result of 10 5/5 without parentheses is 11. Although parentheses affect a combination operand in an expression, they cannot guarantee a special quotation in all situations. For example, crackers of the following expressions and the combination from left to right cannot guarantee what value is in each sub-expression.
(i 1) * (2 i)
The compiler is free to calculate the value of the multiplication between the multiplication of the multiplication in any order. If i's initial value is 0, the entire expression is evaluated in two statements:
(0 1 1) * (2 1)
(0 1 1) * (2 0)
The abnormality caused by side effects is discussed in "side effects" behind this chapter.
Value and R value expressions
It is referred to as the expression of the memory location is called the "L value" expression, and one L value represents a "locator" value of a storage area or "left" value, which implies the left side of the equal sign (=). l value is often an identifier.
Refers to the expression of the right modified position called "modified l value". A modified L value does not have an array type, an incomplete type or a type with a const property, which can be a member of the Const property that can be modified to modify the L value, which must have a member of the const attribute. The name of the identifier indicates a storage location, and the value of the variable is the value stored in this position.
If an identifier points to a memory location, its type is an arithmetic, structural, a combination, or a pointer, then the identifier is a modified L value. For example, if the PTR is a pointer to a storage area, * PTR is a modified L value indicating that the storage area is pointed by the PTR.
Any C expression can be a l value expression:
* An integer, floating point, pointer, structure, or a federated identifier. * A subscript ([]) expression that does not have the value of the group.
* A member selects expression (-> or.)
* A single-dimensional access (*) expression that does not point to an array.
* A l value expression in a parentheses.
* A const object (a L value that cannot be modified).
This "R Value" is sometimes used to describe a value of an expression to distinguish between a L value. All L values are R value, but not all R values are l values.
Microsoft Special Office
C The expansion of the ANSI C standard is to allow the L-value shape as a l value, and the size of the object does not extend through the shape (for more information, see "Type Model Conversion" at the end of this chapter). This feature is illustrated as follows:
Char * p;
Short I;
Long L;
(long *) p = & l; / * legal shape * /
(long) i = L; / * illegal shape * /
C default can expand Microsoft. Use the / zA compiler option to ban this expansion.
Microsoft End
Quantity expression
A constant expression is evaluated at the time of compiling, not at runtime, which can be used anywhere in one constant can be used. A constant expression must be evaluated as a constant within the range that can represent values in this type. A constant expression can be an integer constant, a character constant, a floating point constant, an enumeration, a type shape, a SizeOf expression, and other constant expressions.
grammar
Constant expression:
Conditional expression
Conditional expression:
Logic OR expression
Logic OR expression? Expression: Conditional expression expression:
Assigning expression expressions,
Assignment expression assignment expression:
Conditional expression
Single-eye expression assignment operator assignment expression
Assignment Operator: One of the following:
= * = / =% = = - = << = >> = & = ^ = | =
Structural specifiers, enumerators, direct illustrators, direct abstract descriptors and label statements, non-ends contain constant expressions non-end.
An integer constant expression must be used to specify a size of a structural bitmaster, a value of a constant value, a value of an array or a CASE constant.
Some additional limits are met with constant expressions in the preprocessor command. Results They are well known "limiting constant expressions". A constant constant expression cannot contain a SizeOF expression, an enumeration, to any type of type shape or floating point type constant. But it can include a defined specific constant expression (identifier).
Expression evaluation
The expression involved in assignment, single-grade 1, single grading 1 or call a function has the result of the value of the value (side effect), when arriving at a "order point", anything prior to the order includes any side effects Be saver before the order is started to calculate anything after this order.
Change "side effects" by evaluating value for an expression. Side effects appear at any time the value of a variable is changed by an expression. All assignment operations have side effects. If the function call changes the value of an external visible item by direct assignment or using a pointer, the function call also has side effects.
side effect
An expression of the expression is determined by the specified implementation, except that the language ensures a special value order (as described in the "priority and quotation order" later in this chapter). For example, side effects appear in the following function calls:
Add (i 1, i = j 2);
MyProc (Getc (), getc ());
A function called by a function can be evaluated in any order. Expression I 1 can be evaluated before I = J 2, or i = j 2 can be evaluated before I 1, each case is different, and like, it is impossible to ensure the actual transmission of which character is actually transmitted Give myProc. Since the single-graphic increase 1 and the reduction 1 operation involves assignment, the operation in the following example may result in side effects:
X [i] = i ;
In this example, the value modified X is unpredictable. The value of the subscript can be the new value or old value of I. As a result, it may be different in different compilers or different optimization layers. Since C is uncertain, the order of evaluation, the evaluation method discussed above is correct, or it can be implemented.
To ensure that your code is portable and clear, avoid using its side effects depending on the statement of special evaluation order.
Sequence point
A coherent "order point" between the values of an object can only be changed once by an expression. C language definitions are as follows:
* Logical AND operator (&&)'s left operating number. The left operating number of the logical AND operator has completed the value and completes all side effects before proceeding. If the left operation count value is false (0), the other operand is not evaluated. * Logic OR operator (||) left operating number. The left operating number of the logical OR operator is continued to complete the value and complete all side effects. If the left operation count value is true (non-0), no value for other operands.
* The left operation of the comma operator. The left operation of the comma operator has completed the value and completes all side effects before proceeding. The two operands of the comma operator always evaluate. Note that the comma operator in a function call does not guarantee the order of evaluation. l Function call operator. The order of the request value is not specified in the parameter before entering a function.
* The first operand of conditional operators. The first operand value of the condition operator is completely completed and all side effects are completed.
* A complete initial expression end (i.e., an expression that is not another expression part, for example, a result of an initialization in a description statement).
* A expression in an expression statement. The expression statement is followed by a selection (;) by an optional expression. This expression is evaluated for side effects and keeps with this summary.
* Control expressions in a selection (if or switch) statement. Due to the evaluation of this expression before execution of this selected code, complete all of the side effects.
* A control expression of a While or DO statement. The evaluation value of the expression is completed before any statement in the next iteration of the While or DO loop, and completes all side effects.
* All three expressions in a for statement. The value of the expression is completed before any statement in the next iteration of the FOR cycle, and all side effects are completed.
* Expression in a returnite statement. Complete the expression of the expression before control returning to the calling function, and completes all side effects.
Operator
There are three types of operators. The single-graphic expression is added to an operand consisting of a single operator, or a SizeOf key with an expression, which can be a name of a variable or a model expression. If the expression is a model expression, it must be included in parentheses. A binocular expression consists of two operands plus a double-purpose operator. A three-eyed expression consists of three operating conditions plus connected conditional expression operators. C included the following one-scale operator:
Symbol name
- ~! Reverse and taking a non-operator
* & Indirect access and address operators
SizeOf Size Operator
Single-grade plus operator
- single-grade 1 and minus 1 operator
The binocular operators are combined from left to right. C provides the following binocular operators:
Symbol name
* /% Multiplication operator
- add operator
<< >> Shift operator
<> <=> = ==! = Relational operator
& | ^ Bit Operator
&& || Logic operator
The sequence evaluation operator condition The expression operator has a lower priority than the binocular expression, and is different from them on the right binding. Expression of the operator also includes assignment expressions, which uses a single-graph or a two-eyed assignment operator. Single-grade assignment operators are increasing 1 ( ) and minus 1 (-) operators; the double object assignment operator is a simple assignment operator (=) and a composite assignment operator. Each composite assignment operator is a combination of additional binocular operators and simple assignment operators.
Priority and quotient order
The priority of the C operator and the combination of the combined law affect the combination and evaluation of the operand in the expression. The priority of an operator is only meaningful when there is a higher or lower priority. Expression with high priority operators first evaluate. The priority can also be described by the word "binding". An operator with higher priority is said to have a more tightly coupled.
Table 4.1 summarizes the priority of the C operator and the combination law (order of the operator evaluation), and the priority is high to low. Several operators can only appear together, they have equal priority and evaluate according to their combined law. The operator in this table is in a section starting with the "suffix operator". The remainder of this section gives information about the priority and combination law.
Table 4.1 Priority and combined law of C operator
Symbol operation Type Combine Law [] (). -> Suffix and suffix - expression from left to right prefix and prefix - SizeOf & _ ~! Single from right left type shape from right To the left * /% multiplication from left to right - add from left to right << >> displacement from left to right <> <=> relationship from left to right ==! = Equal from left to right & bits and From left to right ^ Press or from left to right | Bit or left to right && logic AND from left to right || logic OR from left to right?: Conditional expression from right to left = * = / = % = = - = << = >> = & = ^ = | = Simple and composite assignment From the right direction left, order evaluation from left to right 1. operator is listed in priority. If there are several operators in the same line or in the same group, they have the same priority.
2. All simple and composite assignment operators have the same priority.
An expression can include several operators with the same priority. When several such operators appear in one of the same layers of an expression, the resulting value is performed from the right left or left right to right according to the binding law of these operators. The direction of evaluation does not affect the result of such an expression, which may contain multiple multiplication (*), addition ( ) or binocular bits (& | ^) operators in the same level. The order of the priority is not defined by the language. If the compiler can guarantee the consistent result, the compiler can be evaluated in any order.
Only sequential value (,), logical and (&&&&&&&&&), the conditional expression (? :), and function call operator set up a sequence point, thereby ensuring the secondary quotation of these operators. The function call operator is a set of parentheses after the function identifier. The order evaluation operator (,) guarantees that its operator is evaluated from left to right (note that the comma operation symbol is different from the order evaluation operator in a function call, and no such guarantee is provided). For more information, see "Single Point" in front of this chapter.
The logical operator also guarantees their operators from left to right, but they wish to determine the minimum operator of the result of the expression. This is called "short circuit" evaluation. Because some operators of this expression may not be evaluated, for example, in the expression:
X && y
Only x is true (non-0), the second operator Y is evaluated, so if x is false (0), Y does not increase 1.
example
The following table describes how a compiler is automatically binding several basic expressions.
Expression automatic connection
A & B || C (A & B) || C
A = B || C A = (b || c)
Q && r || S - (Q && r) || s -
In the first expression, the priority of the bit and operator is higher than the priority of the logic OR operator (|), and therefore, A & B forms a first operand of logic OR operations.
In the second expression, the logic OR operator (|) has a higher priority than the simple assignment operator (=), so B || C is combined as the right operator in the assignment. Note that the value assigned to a is 0 or 1.
The third expression illustrates a correctly consisting of expressions that may generate an unpredictable result. The logical AND operator (&&) is higher than the priority of the logic OR operator (||), so Q &&r is incorporated into an operand. Since the logical operator guarantees from left to right, Q &&r is evaluated before S - previous. If Q && R is worth 0, S - no longer evaluates, S is not reduced by 1.
If S is not reduced, it may cause a problem that the program is problematic, and S-should appear as the first operand in the expression, or S is reduced in one separate operation.
The following expressions are illegal, generating a diagnostic message at compile:
Illegal expressions default portfolio
P == 0? p = 1: p = 2 (p == 0? p = 1: P) = 2
In this expression, the equal operator (==) has the highest priority, so P == 0 combines into one operand. The conditional expression operator (? :) has a high priority, its first operand is p == 0, the second operand is P = 1. However, the last operand of the conditional expression operator is considered to be P instead of p = 2, which is because the binding of P and the conditional expression operator is more tight than the binding of the composite assignment operator. Because = 2 does not have a left operator, a syntax error occurs. You can prevent this error from appearing and generate more readable code using parentheses. For example, you can use the following parentheses to correct the previous example and make it clearer
(p == 0)? (p = 1) :( p = 2)
Common arithmetic conversion
Most C operators perform type conversion to convert an operand of an expression into a common type or the extension of the extension value is an integer size used in the machine operation. The conversion executed by the C operator depends on the type or operand of the specific operator and operand. However, many operators perform similar conversions on the operands of integers and floating point types, which are well known "arithmetic conversion". An operational value is converted into a compatible type makes its value does not change.
The arithmetic conversion summary below is called "common arithmetic conversion". These steps are applied only to the two-way operators of the arithmetic type and the two operands are not the same type. Its purpose is to generate a public type, which is also the type of final result. In order to determine the actual conversion, the compiler applies the following algorithm to the binocular operation in the expression. The order of the steps is not given by priority:
1. If one of the operands is long Double, other operands are converted to a long double type.
2. If the above conditions are not satisfied, and one of the operands is Double, the other operand is converted to a Double type.
3. If the above two conditions are not met, one of the operands is a float type, other operands are converted to a Float type.
4. If the above three conditions are not satisfied (no operands are floating point types), then the following integer conversion is performed on the operand:
* If one of the operands is the unsigned long type, other operands are converted to the Unsigned long type.
* If the above conditions are not satisfied, one of the operands is the long type, the other is the unsignedint type, then the operand is converted into a UNSigned long type.
* If the above two conditions are not satisfied, one of the operands is the LONG type, the other operand is converted to a long type.
* If the above three conditions are not satisfied, one of the operands is unsigned int, then other operands are converted to the Unsigned Int type.
* If the above conditions are not satisfied, the operands are converted into int types.
The following example illustrates these conversion rules:
Float fval;
Double DVAL;
INTI VAL;
Unsigned long ulval;
Dval = IVAL * ULVAL; / * IVAL uses step 4 to unsigned long
* The result of multiplication is converted into double * /
Dval = ulval fval; / * ULVAL Use Step 3 to Float
* The result of the addition is converted into double * /
Suffix operator
The suffix operator has the highest priority (the most tight binding) in the expression value.
grammar
Suffix expression:
Basic expression
Suffix expression [expression]
Suffix expression [parameter expression table OPT]
Suffix expression. Identifier
Suffix expression -> identifier
Suffix expression
Suffix expression -
The operator in this priority layer has array subscripts, function calls, structures, and joint members, and suffix increases 1 and the suffix minus 1 operator.
One-dimensional array
A suffix expression is followed by a square bracket ([]) to the subscript representation of an element of an array object. A subscript expression represents the value of such an address, its address is that the suffix expression plus the location of the specified value in the expression, indicating:
Suffix expression [expression]
Typically, the value represented by the suffix expression is a pointer value, such as an array identifier and an expression of an integer. But all syntax needs a expression of a pointer type and another is an integer type. Therefore, the integer value can be in the suffix expression location, the pointer value can be in the expression of square brackets or "subscript" position. For example, the following code is legal:
INT SUM * PTR, A [10]; int main {ptr = a; sum = 4 [PTR];} The subscript expression is usually used to point to array elements, but you can apply the subscript to any pointer. Regardless of the order, the expression must be included in square brackets ([]).
The subscript expression is added to the pointer value, and then applied indirect access operators (*) evaluation (discussions on indirect access operators), see "Indirect Access and Additional Address Operators" behind this chapter. . In fact, as the following four expressions are equal, assuming A is a pointer, B is an integer:
a [b]
* (a b)
* (B a)
B [A]
According to the conversion rule of the addition operator (in the "addition operator"), the integer value converts it into an address offset by multiplying it by the length of the pointer address type.
If you hypothesize that the identifier line points to an array of int types, the following procedure is used to calculate the value of the subscript expression ling [i]:
1. The integer value is multiplied by the number of bytes determined by an INT term. The conversion value of i represents I INT position.
2. The value of this conversion plus the initial pointer value (line) produces an address, which is the location of the I int.
3. Apply indirect access operators to this new address. The result is the value of the array element (intuitive LINE [I]).
The subscript expression line [0] shows the value of the first element of Line, since it offers the address represented by Line to 0. Similarly, an expression such as line [5] refers to the elements of the offset LINE to 5 locations or the sixth element of the array.
Multidimensional Arrays
A subscript expression can also have multiple subscripts, as follows:
Expression1 [Expression2] [Expression3] ...
The subscript expression is bonded to the right from left. The leftmost mark expression expression1 [expression2] first evaluates. Its address plus Expression1 and Expression2 constitute a pointer expression; Expression3 plus this pointer expression constitutes a new pointer expression, so until the last subscript expression has been added. An indirect access operator (*) is applied after the last subscript expression, unless the last pointer value is an array type address (see the example below).
Expression with multiple subscripts refers to "multidimensional array". A multi-dimensional array is an array of elements. For example, a first element of a three-dimensional array is a two-dimensional array.
example
For the following example, an array named PROP illustrates three elements, each is an int type 4 × 6 array.
INT Prop [3] [4] [6];
INT I, * IP, (* IPP) [6];
Reference pop arrays:
I = prop [0] [0] [1];
The above example illustrates how to reference the second int elements of the prop. The array is stored in row, so the last subscript is the fastest change; the expression Prop [0] [0] [2] refers to the next (third) element of the array, and the like.
I = Prop [2] [1] [3];
This statement is more complicated to reference the individual elements of the prop. The expression is as follows:
1. The first subscript is 2, multiplied by the size of the 4 × 6 int array, plus the PROP pointer value, which points to the third 4 × 6 array of PROP.
2. The second title is 1, multiplied by the size of the six elements INT arrays, plus the address represented by PROP [5].
3. Each element of the 6 element array is an int value, so the last subscript 3 is multiplied by an int size before adding to PROP [2] [1]. The result pointer is the fourth element of the 6 element array.
4. Apply an indirect access operator to the pointer value. The result is an int element at the address. The following two examples illustrate the case where the indirect access operator is not applied:
IP = Prop [2] [1];
IPP = Prop [2];
In the first statement of the above statement, the expression PROP [2] [1] is a valid reference to the three-dimensional array PROP. It refers to the first 6 element array (described above). Since the pointer value is an array address, indirect access operators cannot be applied.
Similarly, in the second statement IPP = PROP [2], the result of the expression PROP [2] is a pointer value of a two-dimensional array of addresses.
Function call
A "function call" is an expression that contains a name or function pointer value that is called a function or an optional parameter transmitted to the function.
grammar
Suffix expression:
Suffix expression (parameter expression table OPT)
Parameter express form:
Assignment expression
Passage expressions, assignment expressions
The suffix expression must calculate a function address (for example, a function identifier or value of a function pointer), and the parameter expression table is a expression table ("parameter) transmitted to the function (separated expression with comma) ). Parameter expression table parameters can be empty. A function call expression with the value and type returned by this function, a function cannot return an array type object if the function is returned to Void (that is, the function explains
No return value), the function call expression also has a Void type (for more information, see "Function Call" in Chapter 6 "Function").
Structure and joint members
A "member selection expression" refers to the structure and a combined member, such an expression has the value and type of the selected member.
grammar
Suffix expression. Identifier
Suffix expression -> identifier
This table describes two formats of members to select expression:
1. In the first format, the suffix expression represents a value of a struct or UNION type, the identifier named the specified structure or a federated member. The value of this operation is the value of the identifier. If the suffix expression is a L value, the value of the operation is also a l value. For more information, see "L Values and R Value Expressions" in front of this chapter.
2. In the second format, the suffix expression represents a structure or a fixing pointer, the identifier named a member of the specified structure or array. Its value is the value of the identifier and is an L value.
Members have a similar role in two formats of expressions. In fact, the expression involving member selection operators (->) is a speed notked version of the expression, which uses a sentence and the expression before the period consists of indirect access operators acting on a pointer value. So: Expression -> Identifier
Equivalent to:
(* Expression). Identifier
The expression here is a pointer value.
example
The following example refers to such a structural description, and information on the use of indirect access operators (*) in these examples, see "Indirect Access and Reception Address Operators" later in this chapter.
Struct Pair {Int a; int b; struct pair * sp;} item, list [10];
One member of Item selection expression is as follows:
Item.sp = & ITEM In the above example, the address of the ITEM structure is assigned to the SP member of the structure. Such ITEM contains one pointer.
(Item.sp) -> a = 24;
In this example, the pointer expression item .SP uses the member selection operator (->) to assume a value to A:
List [8] .b = 12; This statement explains how to select a single structural member from a structural array.
Human increase 1 and minus 1 operator
The operand of the suffix 1 and the 1 operator is the scalar type of the modified L value.
grammar
Suffix expression:
Suffix expression
Suffix expression -
The result of the suffix increase 1 or minus 1 operation is the value of the operand. After obtaining the result, the operand increases 1 (or minus 1). The following code will indicate the addition of 1 operator.
IF (var > 0)
* p = * q ;
In this example, the variable var is compared to 0, and if the VAR is increasing between 1, the next statement is performed. First, the value of the object referred to by Q is assigned to the object referred to Q, and then Q and P are incremented by 1.
Single-grade operator
The single operator appears before the number of operators and is combined from right to left.
grammar
Monocular expression:
Suffix expression
single-graphic expression
- single-grade expression
Single operator model expression
SIZEOF single-eye expression
SIZEOF (Type Name) Unit Operators: One of the following
& * ~!
Prefix 1 and 1 operator
When an increase 1 and a minus 1 operator appear between operands, the single-grade operator ( and -) is called a "prefix" to increase 1 and minus 1 operator.
The priority of the suffix increases 1 and the reduction 1 is higher than the prefix 1 and the 1 operator is high. The operand must be an integer, floating point, or pointer type, and must be a modified L-value expression (a expression without const attributes). The result is a l value.
When the operator appears before the operand, the operation is increased by 1 or minus 1, and its new value is the result of the expression.
An integer or floating point type increases or decreases integer values 1. The type of result is the same as the operand type.
A pointer type operand increases or reduces the size of the object referred to in the address, an increased 1 pointer points to the next object, a minus 1 pointer pointing forward one object.
example
This example shows a single current embarrassment 1 operator:
IF (Line [--i]! = '/ n')
Return;
In this example, the variable I minus 1 before the subscript as Line.
Indirect access and take address operators
Indirect access operators (*) have access to a value through a pointer. This action value must be a value. The operand must be a pointer value. The result of its operation is the value referred to in this operand, that is, the value indicated by the operand. The type of result is the type of address indicated by this operand.
If the operand points to a function, the result is a function indicator, if it point to a storage location, the result is a L value indicates the storage location.
If the pointer value is invalid, the result is uncertain. Some of the most common conditions that include invalid pointer values as follows:
* This pointer is an empty pointer.
* This pointer indicates an address of a partial item that is not visible during reference.
* This pointer indicates an address that is not suitable for assigning the type of object.
* This pointer indicates an address that the execution program cannot be used.
Take the address operator (&) gives the address of its operand. The number of operands to take the address operator can be a function indicator or a L value indicating an object that is not a bit field and does not describe the REGISTER storage class indicator.
The result of the address operator is the pointer of the operand, and the type of address refers to the pointer is the type of operand.
Take the address operator can only be applied to the basic type, structure, or a federated type, or a reference to the file-wide layer. In these expressions, you can add it in this address expression.
Or minus a constant expression that does not include the address operator.
example
The following example uses three instructions:
INT * PA, X;
Int a [20];
Double D;
The following statement uses the address operator:
PA = & a [5];
The address operator (&) takes the address of the sixth element of the group A, stores the result in the pointer variable PA.
X = * pa;
In this example, an indirect access operator (&) is used to access the INT value stored at the PA address, and the value is assigned to the integer variable x.
IF (x == * & x)
Printf ("True / N");
This example prints the word TRUE to confirm that the result of applying an indirect access operator to the X-x is the same.
Int runk (void); / * function description * /
INT * PROUNDUP = ROUNDUP;
INT * PROUND = & ROUNDUP;
Once the function ROUNDUP is explained, the two pointers of the ROUNDUP also illustrate and initialize. The first pointer Proundup initializes only the name of the function, and the second pointer multou is in initialization to take the address operator. These two initializations are the same.
Single arithmetic operator
In the following table, C is discussed, an arithmetic reflusions, and displaced and logical non-operators.
Operator description
The single-grade operator is located in front of a expression in parentheses enforcement. It is used in an expression involving multiple binding or exchange of binocular operators. The operand must be an arithmetic type. The result is the value of the operand. An integer operand is an integer improvement, and the type of result is the type of increased operand.
- Arithmetic Take the reverse operator generated the retrieval value of this operand (complement of 2). This operand must be an integer or floating point value. This operator performs commonly used arithmetic conversion.
~ Pressing the inverse (or pressing the NOT) operator to generate its operands. This operand must be an integer type. This operator performs common arithmetic conversion, and the result has the type of operand after conversion
! The logical non-(logical NOT) operator generates a value of 0 when the operand is true (non-0), generating a value 1 when the operand is false (0). The result has an Int type. The operand must be an integer, floating point, or pointer value.
One-grade arithmetic operation on the pointer is illegal.
example
The following example will illustrate the single-grade arithmetic operator:
Short x = 987;
X = -x
In the above example, the new value of X is the arithmetic reflusions of 987.
UNSIGNED SHORT Y = 0xAAA;
Y = ~ y; In this example, the new value of Y is the bit position of the unsigned value 0xAAAA to reflect, ie 0x5555.
IF (! (x If x is greater than or equal to Y, the result of this expression is 1 (true). If x is less than Y, the result is 0 (fake). Sizeof operator The SIZEOF operator gives the number of storage required by the object that stores the type of the operand in bytes. This operator allows you to avoid specifying the data size dependent on the machine in the program. grammar SIZEOF single-eye expression SIZEOF (Type Name) This operand is an identifier for a single-graphic expression or type modeling expression (that is, a type indicator is placed in parentheses). This single-graphic expression cannot represent a bit domain object, an incomplete type or a function indicator. The result is an unsigned integer parameter. Standard header file stddef.h defines this type of size_t. When you apply the SIZEOF operator to an array identifier, its result is the size of the entire array rather than the pointer indicated by the array identifier. When you apply this SizeOF operator to a structure or a combination name, or a structure Or when the identifier of the federal type, the result is the structure or a combined number of bytes, including the internal and tail fills. This size can include internal and tail fills used to align structures or federated on memory boundaries. Therefore, the result may not correspond to the result of the stored in all members. If an array without size is a final element of a structure, the SIZEOF operator returns the size of the structure without the array. Buffer = Calloc (100, Sizeof (int)); This example uses the SIZEOF operator to transmit an int size, which may vary in different machines, and transmitted to the name Calloc running function as a parameter. The value returned by this function is stored in the buffer. Static char * strings [] = {"this is string one", "this is string two", "this is string three",} Const int string_no = (sizeof strings) / sizeof strings [0]); In this example, Strings is an array of a char pointer. The member of the pointer is the number of elements in the array, but is not specified. Calculate the number of elements in the array to determine the number of pointers in the number of elements in the array using the SIZEOF operator. Const integer string_no is initialized to this number. Because this is a const value, string_no cannot be modified. Model operator A type model provides a way to explicitly convert an object type in a particular state. grammar Model expression: Single-eyed expression (Type Name) Model Expression After the type of model is forced to convert, the compiler processes the type referring to the type name. Modeling can convert an object of any scalar type into another scalar type. Explicit type styling also complies with the implicit conversion rules of discussion in the "Assigning Conversion" in this chapter. Additional limits on the shape come from the actual size or representation of the specified type. For information on the actual size of the integer type, see "Basic Types of Storage" in Chapter 3, "Description and Types". For more information on type model, see "Type Model Conversion" behind this chapter. Multiplication operator multiplication The operator performs multiplication (*), division (/) and remainder (%). grammar Multiplication expression: Model expression Multiplication Expression * Model expression Multiplication Expression / Style Expression Multiplication Expression% Modeling Expression The operating number of operators (%) must be an integer. Multiplication (*) and division (/) operators can use integer or floating point type operands, the type of operands can be different. The multiplication operator performs common arithmetic conversion on operands, and the type of result is the type of converted operand. Note: Since the conversion executed by the multiplication operator does not provide upper overflow and down overflow conditions, if a multiplication result cannot be represented by the type of operand after the conversion, the information may be lost. The description of the C multiply operator is as follows: Operator description * This multiplication operator causes them two operands multiplied / This divider operator causes the first operand to be removed by the second operand. If the two integers are removed and the result is not an integer, then cut it according to the following rules: * According to the ANSI C standard, the result of 0 is uncertain, and the C compiler generates an error when compiling or running. * If the two operands are positive or no symbols, the result is cut to 0 * If there is a negative number, the result of this operation is less than or equal to the maximum integer of the algebier or is greater than or equal to the minimum integer of the almost, which is implemented (see Microsoft Special Parts below) Operator description The result of the remainder of the operator is the first operating number divided by the remainder obtained by the second operand. When the division is inaccurate, the result is determined by the following rules: * If the right operand is 0, its result is uncertain * If the two operands are positive or no symbols, the result is a positive number * If there is a negative number, the result is not accurate, they are implemented (see Microsoft Special Offer Section below) Microsoft Special Office In a division having an operand, the direction of the cut is directed towards 0. If there is a negative number in the division of the remainder operator, the result is the same as the symbol of the divided (the first operand in the expression). Microsoft Special Office End Example The following description uses the following examples: INT i = 10, J = 3, N; Double x = 2.0, y; This statement uses a multiplication operator: Y = x * i; In this case, the X multiplies the value of 20.0 by i, and the result is a Double type. n = I / j; In this example, 10 is removed by 3, and the result is toward 0, and the integer 3 is generated. n = I% j; This statement is assigned to N is 10 divided by the remainder of 3. Microsoft Special Office The remainder symbol is the same as the divided symbol, for example: 50% -6 = 2-50% 6 = -2, 50 and 2 have the same symbol. Microsoft End Addition operator Add Mode Operator to perform addition ( ) and subtraction (-) grammar Adding expression: Multiplication Addition expression multiplication expression Adding Expression - Multiplication Expression Note: Although the syntax of the addition expression includes multiplication expressions, this does not mean that multiplication needs to be used. For multiplication expressions, model expressions, and single-graphic expressions, see Appendix A "C language syntax summary" later in this book. The operand can be an integer or floating point value. Some additions can also be performed on a pointer value, as outlined in the discussion of each operator. The addition operator performs common arithmetic conversion on an integer and floating point operation. The type of result is the type of conversion operand. Since the conversion executed by the addition operator does not provide an overflow or down overflow condition, if a result of an additional operation cannot be indicated by the type of conversion operand, it may lose information. Addition ( ) The addition operator ( ) causes two operands to add. The two operands can be an integer or floating point type, or an operand and the other operand is an integer. When an integer plus a pointer, the integer value (i) is converted by multiplying it by the size referring to the pointer. After the conversion, the integer value represents the I memory location, and each location has the length specified by its pointer type. When the integer value of the non-converted is applied to the pointer value, the result is a new pointer value to indicate the address of the initial address I position. The new pointer value points to a value of the same type as the initial pointer value, so as the array index (see "one-dimensional array" and "multi-dimensional array" in front of this chapter). If the pointer points to the array range, the result is uncertain in addition to the first position exceeding the highest end. For more information, see "Pointer Arithmetic" behind this chapter. Subtract (-) The subtraction operator (-) minus the second operand from the first operator. The two operands can be an integer or floating point type, or an operand can be a pointer and the other operand is an integer. When the two pointers are subtracted, the difference is converted into a symbol integer value by dividing the size of the value referred to by the pointer. The size of the integer value is defined by the type PTRDIFF_T included in the standard header file stddef.h. The result is a number of memory locations that represent two addresses between the two addresses, which is only meaningful to two elements of the same array, as discussed in "pointer arithmetic". When an integer is subtracted from a pointer value, the subtraction operator is converted by this integer value (i) by the size of the pointer, after the conversion, the integer value represents the I storage location, and each position is There is a length referred to in this pointer type. When the integer value of the conversion is subtracted from the pointer value, the result is the first I am-i memory location. The new pointer points to the value of the type referred to in the initial pointer value. Use addition operator The following example illustrates the plus and reduction operators, use these instructions: INT i = 4, J; Float x [10]; Float * px; The following statement is equivalent: PX = & x [4 i]; PX = & x [4] i; The value of i by by one FLOAT is incremented and added to & x [4], and the result pointer value is the address of X [8]. J = & x [i] - & x [i-2]; In this example, the address of the third element of X is subtracted from the address of the fifth element of X (given by x [i-2]). It is different from the length of a FLOAT, the result is integer 2. Pointer arithmetic The additional operation involved in a pointer and an integer is only the pointer operand pointing to an element of an array and the integer generates the extension in the same array. When the integer value is converted into an address offset, the compiler assumes that only the memory position of the same size is located between the initial address and the result of the amount of deviation. This assumption for array members is valid. By defining, an array is a sequence of the same type of value, and its elements are stored in adjacent memory locations. However, other types of storage cannot be guaranteed by the same type of identifier, that is, the memory location may occur, even if the location of the same type is true. Therefore, the results of adding or subtraction of any value except an array element are uncertain. Similarly, when the two pointer values are subtracted, the conversion assumptions are only the same type of value, no spaces, and the address space given at the operands. By bit displacement operator Displacement operator left shift (<<) or right shift (>>) The number of positions of the first operand, is specified by the second operand. grammar Displacement expression: Addition expression Displacement expression << addition expression Displacement expression >> addition expression The two operands must be an integer value, and these operators perform common arithmetic conversions, and the type of result is the type of left operating after conversion. For the left shift, the right position is set to 0. For the right displacement, the left bit is filld by the type of the first operand after the conversion. If this type is unsigned, they are set to 0; otherwise they are filled with the copy of the symbol bit. For the movement operator to the left, there is no overflow, the statement: EXPR1 << EXPR2 Equivalent to multiply by 2EXPR2. For right-shifted operators: EXPR1 >> EXPR2 If EXPR1 is no symbol or has a non-negative value, it is equivalent to dividing by 2EXPR2. If the second operand is a negative number, or if the right operand is greater than or equal to the width of the bits in the lifted left operating number, the result of a displacement operation is uncertain. Since the conversion executed by the displacement operator does not provide an overflow or down overflow condition, if the result of a displacement operation cannot be represented by the type of the first operand after the conversion, the information may be lost. Unsigned int x, y, z; X = 0x00AA; Y = 0x5500; z = (x << 8) (Y >> 8); In this example, X left moves 8 positions, and the Y is shifted 8 locations. The value of the displacement is added, the result is 0xAA55 and assigns Z. The result of a negative number right shifted is half of its absolute value, for example, -253 (binary 111111000000000011) is shifted to generate -127 (binary 1111111100000000). A positive 253 shifts one bit generates a 126. Right shift symbol bit. When a symbol integer is shifted, the setting the most important bit is retained. This most important bit is cleared when an unsigned integer right is shifted. If the 0xF000 is unsigned, the result is 0x7800. If the 0xF0000000 is a symbol, the right shifts generates 0xF8000000. Move a positive 32 generation to generate 0xF0000000. Move a negative number 32 generation to generate 0xFffffFFFF. Relationship and equality operator The bidewire relationship and the equal operator compare its first operands to the second operand to test the validity of the specified relationship. If the relationship between the test is true, the result of the relationship expression is 1, if it is 0. The type of result is int. grammar: Relationship expression: Displacement expression Relationship expression Relationship expression> Displacement expression Relationship expression <= displacement expression Relationship expression> = displacement expression Equal expressive: Relational expression Equal expressions == Relationship expression Equal expressions! = Relational expression Relationship and equality operator testing is related: The relationship between the operator test > The first operand is greater than the second operand <= The first operand is less than or equal to the second operand > = The first operand is greater than or equal to the second operand == The first operand is equal to the second operand ! = The first operand is not equal to the second operand The beginning of the top four operators have higher priorities than the equal operators (== and! =). See Table 4.1 Priority Information. The operand can be an integer, floating point, or a pointer type. The type of operands can be different. The relationship operator performs common arithmetic conversion on integers and floating point type operands. In addition, you can use the relationship and equality operators to construct a combination of the following operands: * Any of the two operands of any relationship or equal operator can be the same type of pointer. For the result of equal to (==) and not equal to (! =), The comparison indicates whether the two pointers point to the same memory location. For other relational operators (<,>, <= and> =), the results of the comparison indicate the relationship between the two memory locations of the object referred to. The relationship operator is only a relatable offset. The pointer is defined only for the part of the same object. If the pointer points to a member of an array, the comparison is equivalent to the corresponding subscript. The address of the first array element is "less than" the address of the last element. In the case of a structure, the pointer "greater than" the structural member of the structural member described later. The pointer to the same federation is equal. * A pointer value can be compared to the constant value 0 equal (==) or not phase (! =). Pointer with 0 value is called "empty" pointer, that is, it does not point to any valid memory location. * The equal operator complies with the same rules as the relationship operator, but allows additional possible: a pointer can be compared to a constant expression of 0 values or a pointer to the VOID. If both pointers are empty pointers, they are equal. The equal operator compares two sections and offset. example The following example explains the relationship and equality operator INT x = 0, y = 0; IF (x Because X and Y are equal, the expression in this example generates a value of 0. CHAR ARRAY [10]; Char * p; For (p = array; p <& array [10]; p ) * p = '/ 0'; The segments in this example are set for each element of Array as an empty word. Enum color {red, white, green} color IF (col == red) These statements use flag Color to illustrate an enumerated variable named color. At any time, the variable can contain an integer value of 0, 1 or 2, which means that the enumeration Color: is one of the red, white and green, respectively. If the IF statement is executed, if the COL contains 0, any statement that relies on the IF. Bit operator The bit operator performs bit bits and (&), pressing or (^) and bit bit or (|) operations. grammar AND Expression: Equal expression AND Expressions & equality expressions Tone or expressions: And expression Different or expressions ^ and expressions OR expression: Tone or expressions OR expression | alternating or expression The operand of the bit operator must have an integer type, but their types may be different. These operators perform common arithmetic conversion. The type of result is the type of conversion operand. The C By operator is described as follows: Operator description The & bit and operator compares each bit of the first operand with the corresponding number of second operands. If the two bits are 1, the corresponding result bit is set to 1; otherwise, the corresponding result bit Set to 0. ^ Compare each bit of the first operand with the corresponding number of the first operands, and if a bit is 0 and the other bit is 1, the corresponding result bit is set to 1, otherwise , The corresponding result bit is set to 0 The bit bit OR operator compares each bit of the first operand to the corresponding number of second operands, if there is a bit 0, the corresponding result bit is set to 1; otherwise, the corresponding result bit setting 1 example These descriptions are used in three examples: Short i = 0xAb; Short i = 0xAbcd; Short n; n = I & j; The result of assigning N in the first example is the same as the value of I (hexadecimal 0xAb00). n = i | j; n = i ^ j; In the second example, the result of the result is 0xAbcd (hexadecimal number), and the bit is different in the third example or 0xCD (hexadecimal number). Microsoft Special Office The result of bitter operation on the symbol integer is defined according to the ANSI C standard. For the C compiler, the bitwise operation on the symbol integer is the same as the bitwise operation on the unsigned integer. For example, -16 & 99 can be expressed as binary: 11111111 11110000 & 00000000 01100011 ------------------- 00000000 01100000 The result of this bitit is a decimal 96. Microsoft End Logical operator logical operator performs logical and (&&&) and logic OR (||) operations. grammar Logical and expression OR expression Logical AND Expression && OR Table Dare OR expression: Logical and expression Logical and expression || logical and expression The logical operator does not perform a common arithmetic conversion. Instead, they evaluate each operand according to the equivalence of its 0, and the result of a logical operation is 0 or 1, and the type of the result is int. The C logical operator is described below: Operator description && If the two operands are non-0 values, the logical AND operator generates value 1. If there is an operand 0, the result is 0. If a logical AND operation is equal to 0, the second operand does not ask || Logic OR operator executes an OR operation on its operand. If the two operands are 0 values, the result is 0. If there is an operand to be a non-0 value, the result is 1. If the first operator of a logical OR operator is a non-0 value, the second operand does not evaluate the operand of the logic AND and logic OR expression from left to right. If the value of the first operand can be sufficient to determine the result of the calculation, the second result number does not perform the value, which is referred to as "short circuit evaluation". There is a sequence point after the first operand. See "Single Site" in front of this chapter for more information. The example will explain the logical operator as follows: INT W, X, Y, Z; IF (x Printf ("X IS LESS THAN Z / N"); in this example, if the X is smaller than Y and Y is smaller than z, call a PRINTF function to print a message. If x is greater than Y, the second operand (Y Printf ("% d", (x == W || x == y || x == z)); In this example, if X is equal to W and Y or Z, the second evaluation value of the Printf function is true to print 1; otherwise, 0 is printed when evaluates. As long as one of the conditions is true, the value stops. Conditional expression operator C has a three-dimensional operator: conditional expression operator (? :). grammar Conditional expression: Logic OR expression Logic OR expression? Expression: conditional expression Logic OR expressions must have an integer, floating point, or pointer type. The value is performed according to the equivalent value of 0. A sequence point keep up with logic OR expression. The operational number of operations is as follows: * If the logic OR expression is not equal to 0 expression request. This result is given by non-endarators (this means that the expression is only valued only in the logic OR expression.). * If the logical OR expression is equal to 0, the conditional expression is evaluated. This result is the value of its conditional expression (this means that only the logical OR expression is evaluated to the conditional expression). Note that one of the expressions or conditional expressions, but not both. The type of a conditional expression result is dependent on the type of expression or conditional expression operand as follows: * If the expression or conditional expression has an integer or floating point type (their types can be different), the operator performs common arithmetic conversion. The type of the result is the type of converted operand. * If the expression and conditional expression have the same structure, a combination, or pointer type, the result type is the same structure, a fix, or a pointer type. * If both operands have a Void type, the result is a Void type. * If one of the operands is a pointer for any type of object, the other operand is a void pointer, which is a pointer to a VOID, which is a VOID pointer. * If one of the expressions or conditional expressions is a pointer, the other operand is a constant expression having zero. The type of the result is the pointer of the VOID. In the type of pointer, any type of modifier (const or volatile) in the type referred to in this pointer is not important. However, the result type inherits the modifier with the components of two conditions. example The following example explains the use of the conditional operator J = (i <0)? (-i): (i); This example assigns the absolute value of I to J. If i is less than 0, the -i is assigned to j; if i is greater than or equal to 0, i is assigned to j. Void F1 (void) Void F2 (Void); INT X; int y; (x == y)? (f1 ()): (f2 ()); In this example, two functions F1 and F2 and two variables x and y are illustrated. After the program, if the two variables have the same value, the F1 function is called; otherwise the F2 function is called. Assignment operator A assignment calculates the value of the right operand to the storage location named on the left operand. Therefore, the left operand of an assignment must be a modified L value. After assignment, an assignment expression has a value of the left operand, but is not a L value. grammar Assignment expression: Conditional expression Single-eye expression assignment operator assignment expression Assignment operator: one of the following = * = / =% = = - = << = >> = & = ^ = | The assignment operator in c can be converted and assigned in a single operation. C provides the following assignment operator: Operator execution = Simple assignment * = Multiplication assignment / = Division assignment % = Yu number assignment = Addition assignment - = subtraction assignment << = Left bond assignment >> = Right displacement assignment & = Boot and assignment ^ = Pressibility or assignment | Bit or assignment In assignment, the type of right value is converted to the type of left value and stores this value in the left operand after assignment. The left operand cannot be an array, a function or constant. The specified conversion path is discussed in detail in "Type Conversion" behind this chapter, which depends on two types. Simple assignment The simple assignment operator assigns its right operand to the left operand. The value of the right operand is converted to the type of assignment expression and replaces the value stored in the object specified by the left operation. Application assignment conversion rules (see "Assigning Conversion" behind this chapter). Double X; Int Y; X = Y; In this example, the value of Y is converted to a Double type and assigns X. Composite assignment Composite assignment operators combine simple assignment operators and other binocular operators. The composite assignment operator performs the calculation specified by additional operators, and then assigns the result to the left operation. For example, such a compound assignment expression: Expression1 = Expression2 Can understand Expression1 = Expression1 Expression2 However, the composite assignment expression is not equal to the expansion, because in the addition operation and in the assignment operation, the composite assignment expression only evaluates EXPRESSION1 once, and the extended version evaluates EXPRESSION1 twice. The operand of a composite assignment operator must be an integer or floating point type. Each composite assignment operator performs a conversion executed by the corresponding binocular operator, and limits the type of the operand. The addition assignment ( =) and subtraction assignment (- =) operators also have a pointer type left-way operating number, in which case the right operand must be an integer type. The result of a composite operator has the value and type of left operating. #define mask 0xff00 N & = MASK; In this example, a bit bit-bit AND operation is performed on N and Mask, and the result is assigned to n. Explicit constant Mask defines a #define preprocessor command. Sequential evaluation operator The order evaluation operator is also called a "comma operator", calculates its two operands from left to right order. grammar expression: Assignment expression expression, Assignment Expression Sequence The left operating number of the value operator is evaluated as a VOID expression. The result of this calculation has the same value and type as the right operand, each of which can be any type. The order evaluation operator does not perform any type conversion between its operands, it does not generate a l value. A sequence point is generated after the first operand, which means that both the side effects generated from the left operation count have been completed before the right operand value begins. For more information, see "Single Point" in front of this chapter. Sequential value operators are typically used to evaluate two or more expressions in context, and only one expression is allowed here. The comma is used as a separator in some contexts, but you have to be careful not to use it as a separator and confused as an operator. Both use are completely different. example This example shows the order evaluation operator: For (i = j = 1; i j <20; i = i, j -); in this example, each operand of the third expression of the FOR statement is separate. Left operation Number I = i first evaluates, then the right operand J - evaluation value. Func_one (X, Y 2, Z); FUNC_TWO ((x -, y 2), z); In the FUNC_ONE function call, three comma-separated parameters are transmitted to: x, y 2, and z, respectively. In the FUNC_TWO function call, the parentheses enforcement compiler explains the first comma as the order evaluation operator. This function call transmits two parameters to func_two, the first parameter is the result of the order evaluation operation (x -, y 2), which has the value and type of Y 2 expression, the second parameter is Z. Type conversion Type conversion depends on the type of operator and operand or operator. Type conversion is performed in the following cases: * When a type of value is assigned to different types of variables, or an operator converts an operand or multiple operands of the number of operands before executing the operation. * When a type of value showed the display of a variety of values to a different type. * When a value is transmitted to a function as a parameter or when a type is returned from a function. * A number of characters, a short integer, an integer domain, all objects with symbolic or unsigned or enumerated types can be used in an expression that can use integers. If an int can represent all the values of all initial types, then the value is converted to int, otherwise it is converted into unsigned int. This process is called "integer improvement". Integer improvement, that is, the value guarantees the same value before the increase is the same. See "Common Arithmetic Conversion" in front of this chapter for more information. Assignment conversion In assignment, the type of assignment is converted into a type of a variable that accepts assignment. C allows the integer and floating point types to be converted by assigning, even if information is selected in the conversion. The conversion method used depends on the type involved in the assignment, as "common arithmetic conversion" and the following section describe: * Transition from the symbol integer type * Conversion from no symbol integer type * Conversion from floating point type * Conversion and transition from pointer type to pointer type * From other types of conversion type modifiers do not affect the allowableness of the conversion, although a const value cannot be used as the left side of the assignment. Conversion from the symbol integer type This value does not change when a symbolic integer is converted into an unsigned integer with equal or more dimensions, and the symbol integer is not negative. The symbolic integer is expanded by symbols, and a symbolic integer is converted into a shorter symbol integer through the high-end bit, and the result is interpreted as an unsigned integer, as examples: INT i = -3; UNSIGNED SHORT U; u = i; Printf ("% hu / n", u); / * Print 65533 * / When a symbolic integer is converted into a floating point type, there is no information loss, except that a longint or unsigned long int value is converted to a FLOAT value that may lose certain precision. Table 4.2 summarizes the conversion of the symbol integer type. This table assumes that the default char type is a symbol. If you use a compilation option to change the default char type as unsigned, this conversion is given in Table 4.3, that is, the application unsigned char type instead of the conversion in Table 4.2. Table 4.2 Symbol Integer Type Conversion From the method charshort sign extension charlong sign extension charunsigned char retention mode, the loss of high-end as a sign bit charunsigned short sign extension is short, converting short to unsignedshortcharunsigned long sign extension is long, convert long to unsigned longcharfloat sign extension is long, convert long as floatchardouble sign extension for the long, long converted to doublecharlong double sign extension for the long, long converted to low-end doubleshortChar reserved byte shortlong sign extension shortunsigned char reserved byte shortunsigned short retention mode of low-end, high-end loss as a sign bit bit shortunsigned long symbol Expanded to Long, convert long to unsigned longshortfloat symbols to long, convert long to floatshortdouble symbols to long, convert long to DoubleShortlong Double symbols to Long, convert longshort to DoublelongChar reserved low-end byte LongshiD Char Reserved low-end word longunsigned char Low-end byte LongunSigned short reserves the low-end LONGUNSIGNED long reserved mode, lost high-end as a symbol bit bit longfloat as Float, if long does not accurately indicate, lost accuracy longdouble as Double, if long can't be used as a Double, lost Precision Longlong Double as Double, if long cannot be accurate as a Double, lost accuracy 1. All char types assume that the CHAR type is marked. Microsoft Special Office For a Microsoft 32-bit C compiler, an integer is equivalent to long. An INT value conversion process is the same as long. Microsoft End Conversion from unsigned integer type A unsigned integer is converted into a shorter or symbolic integer by cutting the high-end bits, or converted into a longer no symbol or symbolic integer by 0 expansion (see Table 4.3). When the value of the integer type is downgraded to a symbol integer, or an unsigned integer is converted to its corresponding symbol integer, if it can represent it in the new type, it does not change its value. But if the symbol bit is set, it represents the value changes, as follows: Int J; UNSIGNED SHORT K = 65533; J = K; Printf ("% hd / n", j); / * Print -3 * / If it does not represent, the result is implemented. For more information on the C compiler to handle integer degradation, see "Type Model Conversion" later in this chapter. Same from integer types or transition from the integer type of shape. The unsigned value can only be converted in a manner that retains their values, and it is not directly represented in C. The only exception is converted from unsigned long to float, which is lost. Otherwise the reserved value, whether there is a symbol or no symbol. When a value of an integer type When converting into floating point, the value exceeds the scope, and the result is unpredictable (information about the range of integers and floating point types, see "Basic Type Storage" in Chapter 3, "Description and Types). Table 4.3 summarizes the conversion from unsigned integer types. Table 4.3 Conversion from no symbol integer type unsigned charchar reserved bit patterns to the method, high-order bit into a sign bit unsigned charshort0 unsigned charlong0 expansion expansion expansion unsigned charunsigned short0 unsigned charunsigned long0 converted to unsigned charfloat long extension, to convert long floatunsigned chardouble converted to a long, long to convert doubleunsigned charlong double converted into a long, long retains the low-end conversion byte unsigned shortshort reserved bit mode doubleunsigned shortchar, high-end position becomes the sign bit unsigned shortlong0 expansion unsigned shortunsigned char retains the low-end byte unsigned shortunsigned long0 expansion unsigned shortfloat converted to a long, long converted to floatunsigned shortdouble converted to a long, long to convert doubleunsigned shortlong double converted to a long, low-end long reserved conversion doubleunsigned longchar unsigned longShort bytes reserved reserved bit unsigned longlong low word mode, high-order bit into a sign bit unsigned longunsigned char retains the lower end Byte UNSIGNED LongunSigned Short Reserved Low Terminal Unsigned Longfloat Convert to Long, convert long to floatunsigned longdouble directly to DoubleunSigned longlong double converted to Long, convert long for DoubleMicrosoft special place For Microsoft 32-bit C compilers, the unsigned int type is equivalent to the Unsigned long type. The way a unsigned int value is used in the same way as a unsigned long. If the value of the conversion is greater than the maximum symbolless long value, it is not accurate from the unsigned long value to FLOAT. Microsoft End Conversion from floating point type A float value can be converted into a Double or Long Double, or a Double converted into a long double without changing the value. If possible, a Double value is converted to a precise Float value. If this value cannot be accurately indicated, it may lose the accuracy. If the result is exceeded, its behavior is unsure. For the scope of the floating point type, see "Restriction of floating point constants" in Chapter 1, "C". By converting a floating point value to a long, then converting the long value into the specified integer value, as explained in Table 4.4, convert the floating point value into an integer value, converted into long, discarding the The fractional portion of the floating point value. If it is still too large, it is not suitable for the LONG, the result of the conversion is uncertain. Microsoft Special Office When a Double or Long Double floating point number is converted into a smaller floating point number, the value of the floating point number is cut forward towards 0 if overflow occurs. One overflow causes a run error. Note that the C compiler maps long double to double. Microsoft End Table 4.4 summarizes the conversion from floating point types. Table 4.4 Conversion from floating point types From the way floatchar converted to long; converting long for CharfloatShort to long; converting Long is cut with shortfloatlong at a decimal point. If the result is too large to represent, the result is that the unseasive floatunsigned short is converted to long. Convert long to unsigned shortfloatunsigned long to long, convert the long to unsigned longfloatdouble, indicating that the floatlong chouble change inside the internal representation DoubleChar converted to float, Convert FLOAT to CHARDOUBLESHORT to FLOAT, convert Float to ShortdouBLONG cut at the decimal point. If the result is too big to use long, the result is that the DoubleunSigned Short converted to Long, converting long to unsigned shortdoubleunsigned long to long, conversion long is represented as Float for unsigned longdoublefloat. If the Double value cannot be accurately represented by the float type, accuracy is lost. If the value is too big to say as a float, the result is uncertain LONGDOUBLECHAR to float, convert Float to float to float, convert Float to shortlongDoublelong at a decimal point. If the result is too big to use long, the result is an uncertain LONGDOUBLEUNSIGNEDSHORT converted to long; converting long-converted shortlongdoubleunSignedLong to long Doul; conversion long is a Float for unsigned long -doublefloat. If the Double value cannot be accurately represented by the float type, accuracy is lost. If this value is too big to be a float, the result is an uncertain LONGDOUBLEDOUBLE This long Double value is used as Double Processing If the value converted is greater than the maximum positive LONG value, the conversion of Unsigned Long from FLOAT, DOUBLE or Long Double value Is not accurate. Convert to pointer type and switch from pointer type A pointer for a value type can be converted into a different type of pointer. Although this result may result in unity due to alignment requirements and different types of storage sizes. An object's pointer can be converted into a pointer to a certain or identical object that requires less or identical objects, which is not changed. A Void's pointer can be converted to a pointer or from any type of pointer to a pointer of a VOID. If the result is turned over to the initial type, restore the initial pointer. If a pointer is converted into another pointer, the latter has the same type, but there are different or additional modifiers, the new pointer is the same as the old pointer, except for the new modifier. A pointer value can also be converted into an integer value. According to the following rules, its conversion path depends on the size of the pointer and the size of the integer type: * If the size of the pointer is greater than or equal to the size of the integer, the behavior of the pointer is like a no sign value in the conversion, except that it cannot be converted to a floating point value. * If the pointer is smaller than the integer type, first convert the pointer into a pointer that is the same size as an integer type, and then convert into an integer type. Conversely, an integer type can also be converted into a pointer type according to the following rules: * If the integer type has the same size as the pointer type, simply convert this integer value as a pointer (a unsigned integer). * If the size of the integer is different from the size of the pointer type, the integer type is first converted to the size of the pointer, where the conversion route given in Table 4.2 and Table 4.3; then processes it as a pointer value. A expression having a 0 type integer constant expression or a forced shape to type VOID * can be converted by a type shape, assignment, or comparing any type of pointer. This produces an empty pointer equal to another same type of empty pointer, but this empty pointer is not equal to any of a function or an object. Other integers not constant 0 can also be converted into a pointer type, but the result is not portable. Conversion from other types Since a ENUM value is a defined int, the conversion or transition to the ENUM value is the same as the INUM value. For the C compiler, an integer is the same as one long. Microsoft Special Office The conversion is not allowed between the structure or the combination. Any value can be converted to type VOID, but the result of this conversion is only used in an expression value being discarded, for example in an expression statement. From the definition, the Void type is no value, so it cannot be converted into any other type, and the other type cannot be converted into a void, but you can explicitly convert a value as type Void, just as the following "Type Model Conversion" discussed. Microsoft End Type styling conversion You can use the type of model explicit conversion type. Grammatical modeling expression: Single-eyed expression (Type Name) Model Expression type name: Indicator modifier table abstract specifier OPT Type name is a type, and the modeling expression is to convert to this type of value. A expression with type shape cannot be a l value. Model expressions are variables that are converted to the type name specified by the type name. The conversion rule (described in front "Assistance Conversion" is also applicable to type modeling. Table 4.5 illustrates a type that can be converted into a given type. Table 4.5 Legal type shape Destination Type Possible Source Ratings Any Integer Type, Floating Point Type, or a Pointer Floating Point Type of Object No Arithmetic Type A Pointer or (Void *) Any Integer Type, (Void *), a pointer to the object Or a function pointer function pointer any integer type, Void type is any type Any identifier can be switched to Void, but if the specified type in a type model is not a Void, then the identifier shape is converted to a type of VOID expression. Any expression can be styled to Void, but a type of VOID cannot be converted to any other type. For example, a function with a VOID type cannot have another type of return value. Note that a void expression has a Void type pointer instead of type Void. If an object type is a Void type, the result expression cannot be assigned to any item. Similarly, a type of modeling object is not an acceptable L value, so it cannot be assigned to a type of model. Microsoft Special Office As long as the size of the identifier does not change, a type model can be an L value expression. For information on the L value, see the "l value and r value expression" at the beginning of this chapter. Microsoft End You can convert an expression into a type VOID with a shape, but the result expression can only be used to do not need a value. A object pointer converted to a void *, which will return its initial value when converting to the initial type. The transformation type executed on a function call in a function call relies on a function prototype (forward description) used to illustrate the parameter of the called function. If there is a function prototype including the parameter type, the compiler performs type detection (see Chapter 6 "Function"). If there is no function prototype, the common arithmetic conversion is performed on the function call. These conversions are executed independently on each parameter in the call. This means that a FLOAT value can be converted to a double value; a char or short value can be converted to an int; a unsignedchar or unsigned sort can be converted to a unsigned int.