C Language Learning Tutorial Chapter II-Data types, operators, expressions (vi)

Source: Internet
Author: User
Tags arithmetic arithmetic operators bitwise expression logical operators printf

Force type conversions
Coercion of type conversions is done through type conversion operations. The general form is: (type specifier) (expression) whose function is to cast the result of an expression to a type that is represented by a type description represented descriptor. For example: (float) A converts a to solid (int) (x+y) to convert a x+y result to an integral type. You should be aware of the following issues when using casts:
1. Type specifiers and expressions must be bracketed (a single variable can be without parentheses), such as x+y (int) (int) x+y to convert x to an int and then add to Y.
2. Whether it is a casting or an automatic conversion, it is only a temporary conversion of the data length of a variable for the purposes of this operation, without changing the type defined on the variable when the data is described.
Main ()
{
float f=5.75;
printf ("(int) f=%d,f=%f\n", (int) f,f);
}
f<--5.75
Cast float F to int f float f=5.75;printf (int) f=%d,f=%f\n, (int) f,f); This example shows that although F is forced to be an int type, it only works in the operation, is temporary, and the type of F itself does not change. Therefore, the value of (int) F is 5 (the decimal is deleted) and the value of F is still 5.75.

Basic operators and expressions

Type, precedence, and binding of operators
The number of operators and expressions in the C language is rare in advanced languages. It is the rich operators and expressions that make the C language function perfect. This is also one of the main features of C language.
The C language operator not only has different priority, but also has one characteristic, is its binding nature. In an expression, the order in which the operands participate is not only governed by the precedence of the operator, but also by the binding of the operators, so as to determine whether to operate from left to right or from right to left. This binding is not the same as the operators of other advanced languages, thus increasing the complexity of the C language.

Types of Operators C-language operators can be grouped into the following categories:
1. Arithmetic operators
For all kinds of numerical operations. Including plus (+), minus (-), multiply (*), in addition to (/), residual (or modulo operation,%), self-increase (+ +), since minus (-) a total of seven kinds.
2. Relational operators
For comparison operations. Includes six species greater than (>), less than (<), equal to (= =), greater than or equal (>=), less than or equal (<=), and not equal to (!=).
3. Logical operators
Used for logical operations. including with (&&), or (| |), NON (!) Three species.
4. Bitwise operator operator
The amount of the participating operation, which is calculated by bits. Includes bits and (&), bits or (|), bits (~), bitwise XOR, or (^), left (<<), right Shift (>>) six kinds.
5. Assignment operator
For assignment operations, it is divided into simple assignment (=), compound arithmetic Assignment (+=,-=,*=,/=,%=) and compound bit operation Assignment (&=,|=,^=,>>=,<<=) three class altogether 11 kinds.
6. Conditional operators
This is a three-mesh operator for conditional evaluation (?:).
7. Comma operator
Used to combine a number of expressions into an expression (,).
8. Pointer operators
For content (*) and address (&) two operations.
9. Find the number of bytes operator
The number of bytes (sizeof) used to compute the data type.
10. Special Operators
with parentheses (), subscript [], member (→,.) and several.

Priority and Binding
In C, the operator's operation priority is divided into 15 levels. 1 is the highest, 15 is the lowest level. In an expression, higher precedence is performed before a lower priority. At the same time as the operator priority at both sides of an operand, the binding direction is treated according to the binding of the operator. C language in the integration of the operators are divided into two, that is, left-bound (from left to right) and right binding (from right to left). For example, the combination of arithmetic operators is from left to right, that is, first left and right. If the expression x-y+z then y should first be combined with the "-" number, perform the X-y operation, and then perform the +z operation. This combination direction from left to right is called "left binding". The direction of the combination 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, you should first perform y=z and then perform x= (y=z) operation. C language operators have many of the right binding, should pay attention to the difference to avoid understanding errors.

Arithmetic operators and arithmetic expressions basic arithmetic operators
1. The addition operator "+" addition operator is a binocular operator, that is, two quantities should be involved in the addition operation. such as a+b,4+8 and so on. Has the right binding nature.
2. Subtraction operator "-" subtraction operator is a binocular operator. But "-" can also be used as a negative operator, at this time for the monocular operation, such as-x,-5 and so on with the left binding.
3. Multiplication operator "*" binocular operation, with left binding.
4. The division operator "/" binocular operation has a left binding. Participation in the calculation of the whole type, the result is also integral type, give away the decimal number. If one of the operands is a solid, the result is a double-precision solid type.
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 the left binding nature. Participation in the calculation of the whole type, the result is also integral type, give away the decimal number. If one of the operands is a solid, the result is a double-precision solid type. printf ("\n\n%d,%d\n", 20/7,-20/7);
printf ("%f,%f\n", 20.0/7,-20.0/7);
In this case, the results of the 20/7,-20/7 are all integers, and the decimal places are all out. and 20.0/7 and -20.0/7 because of the real number involved in the operation, so the result is also solid type.
5. The remainder operator (modulo operator) "%" binocular operation, with left binding. The amount required to participate in the operation is an integral type. The result of the remainder operation is equal to the remainder after dividing the two number.
void Main () {
printf ("%d\n", 100%3);
}
Binocular operation, with the left of the binding. The remainder operator% requires that the amount of the participating operation be an integral type. This example outputs 100 divided by 3 of the remainder 1.

Self-increasing 1, self minus 1 operator
The 1 operator is recorded as "+ +", and its function is to make the value of a variable increase by 1. The 1 operator is recorded as "--" with the function of reducing the value of the variable by 1. Since the increment of 1, the self minus 1 operators are all monocular operations, all of them have right binding. There are several forms: ++i I will be involved in other operations since the increase of 1. I will take part in other operations since minus 1.
i++ I participates in the operation, the value of I increases itself by 1.
i--I participates in the operation, the value of I is again reduced by 1.
i++ and I--。 easy to make mistakes in understanding and using Especially when they are in a more complex expression or statement, it is often difficult to understand, and therefore 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<--8
I<--i+1
I<--i-1
I<--i+1
I<--i-1
I<--i+1
i<--i-1 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--);
The initial value of I is 8
The 2nd line I plus 1 after the output is 9;
The 3rd line minus 1 after the output is 8;
Line 4th output I is 8 plus 1 (for 9);
Line 5th output I is 9 and then minus 1 (8);
6th line output-8 plus 1 (9);
Line 7th output-9 minus 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<--0
I+i+i--->p,i+1-->i,i+1-->i,i+1-->i
j+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, the p= (i++) + (i++) + (i++) should be understood to be three I addition, so the P value is 15. Then I again 13 times the equivalent of plus 3 so i the last value of 8. And for the value of Q is not, q= (++j) + (++J) + (++J) should be understood as Q first 1, and then participate in the operation, as Q since the increase of 13 times after the value of 8, three 8 of the sum of the 24,j and the final value is still 8. An arithmetic expression expression is a combination of constants, variables, functions, and operators. An expression has a value and its type, which is equal to the value and type of the result of the calculation of an expression. Expression evaluation is performed in the order specified by the precedence and binding of the operators. A single constant, variable, function can be considered a special case of an expression.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.