C Language Basics Tutorial (my c tour started) [Nine]_c language

Source: Internet
Author: User

+ 、-、 *,/, = Priority

1. Priority level

As with mathematics, C language stipulates multiplication and subtraction before adding and reducing . In other words, the multiplication and division operators have a higher precedence (precedence) than the addition and subtraction operators. At the same time, the C language also stipulates that if two operators have the same precedence, and that they are not separated by an operator whose precedence is higher or lower, their order of operations depends on the sequence in which they appear in the statement . Most operators operate from left to right , although there are also right-to-left operations (for example, assignment operators). The multiplication and division operators have the same precedence, and the addition and subtraction operators have the same precedence. Therefore, the following statement

var = 8.0 + 20.0/4.0 * 2.0;

The order of operations is:

20.0/4.0
5.0 * 2.0 (20.0/4.0 5.0)
8.0 + 10.0
var = 18.0

In this expression, the/and * Priority is the same, and it is performed from left to right , so the 20.0/4.0 is first calculated, then the turn is 5.0 * 2.0.

If we want the addition to proceed first, we can add parentheses to 8.0 + 20.0:

var = (8.0 + 20.0) /4.0 * 2.0;

The order of operations for this statement is:

8.0 + 20.0
28.0/4.0
7.0 * 2.0
var = 14.0

C language, the first of the parentheses inside the operation , after the parentheses outside the operation. In parentheses, the order of operations is the same as discussed above. For example:

var = (8.0 + 20.0/4.0 * 2.0)/3.0;

The order of operations is:

20.0/4.0
5.0 * 2.0
8.0 + 10.0
18.0/3.0
var = 6.0

The following table summarizes the precedence of these operators and their associativity, ranking from high to low by priority

operator Binding Law

() from left to right
+-(Monocular) from right to left
*/From left to right
+-(two mesh) from left to right
= from right to left


2. Priority and Operation order

operator Precedence (Operator precedence) is an important rule in determining the order of operations, but it cannot be completely (and not necessarily complete) to determine the order of operations. For example:

5 * 3 + 8 * 4;

Based on operator Precedence, we know that multiplication operations precede the addition Operation . But 5 * 3 and 8 * 4 who first, we are not sure. The sequence of their operations is determined by the compiler . This is because the order of operations is more efficient in some systems, and the other is more efficient in another system. Regardless of their operation, the final result is 47.

You might say, "does multiplication not operate from left to right?" This is not to say that the left-most multiplication first? "Yes, multiplication does operate from left to right, but you also need to see that these two multiplication operators are separated by the addition operator !" We give an example to illustrate the meaning of multiplication from left to right. The following statement

5 * 2 * 9 * 4;

The order of operations is:

5 * 2
10 * 9
90 * 4


Here we look at a small program.

/ * PRECEDENCE.C--Priority Test * *
#include <stdio.h>

int main (void)
{
int var1, VAR2;

var1 = VAR2 =-(9 + 4) * 5 + (6 + 8 * (7-1));
printf ("var1 = Var2 =%d\n", var1);

return 0;
}

Please read the above program carefully, think about what will happen, and then compile and run to see if the results are the same as you think.

First, the parentheses operator has the highest precedence. But (9 + 4) and (6 + 8 * (7-1)) The sequence of operations is determined by the compiler. Suppose (9 + 4) is performed first, then the operation is 13, then the minus operator acts on 13-13. So we get:

var1 = VAR2 =-13 * 5 + (6 + 8 * (7-1));

In (6 + 8 * (7-1)), the first operation (7-1) is:

var1 = VAR2 =-13 * 5 + (6 + 8 * 6);

Because * priority is higher than +, we get:

var1 = VAR2 =-13 * 5 + (6 + 48);

Then

var1 = VAR2 =-13 * 5 + 54;
var1 = VAR2 =-65 + 54;
var1 = VAR2 =-11;

Because the assignment operation is Right-to-left, 11 is assigned to VAR2, and then the VAR2 is assigned to VAR1. The end result is that var1 and var2 are equal, and their values are-11.


25. Modulo addition operator%

% is the modular addition operator (modulus Operator), which is used to find the remainder . % can be used only for modulo integers and not for floating-point numbers. For example:

% 2 //correct. The remainder is 1
15.2% 3 //Error!

Before C99, there was no rule that if there were negative numbers in the operands, what would be the result of the modulo. C99 stipulates that if the operand to the left of% is a positive number, the result of modulo addition is positive; if the operand to the left of% is a negative number, the result of the die addition is negative. For example:

% 2 /1
%-2 /1
-15% 2 /+ 1
-15%-2 /1

The standard stipulates that if A and B are integers, a% B can be calculated with formula A-(A/b) * B. For example:

-15 2 = = 15-( -15/2) * 2 =-15-(-7) * 2 = 1

Finally, let's look at a small program.

/ * MONTHS_TO_YEAR.C-Converts the number of months entered by the user to the number of adults and months * *

#include <stdio.h>

int main (void)
{
int months, years, months_left, months_per_year = 12;

printf ("Enter The number of months:");
scanf ("%d", &months);

years = Months/months_per_year; / * Calculate the number of years * *
Months_left = months% Months_per_year; * * Calculate the remaining months /

printf ("%d months is%d years,%d months.\n", months, years, months_left);

return 0;
}

26. Self-increasing operators and self-subtraction operators

1. Self-increasing operator (Increment Operator)

The increment operator + + increases the value of the operand by 1. + + can be placed in front of the operand or in the back. For example:

++n;
n++;

The result of both statements is to increase n by 1, which can be said to be no different. The same effect is achieved using the following statement:

n = n + 1;

Despite the above two statements, there is no difference between + + front and rear. However, there is a difference between the + + front and the back-placement. For example:

int n = 1, post, pre;

Post = n++;
Pre = ++n;

For post = n++; This statement, the value of n is assigned to post, n is only increased by 1. That is, after the statement is executed, the value of the post is 1, and the value of N becomes 2. and pre = ++n; This statement, N, first increases by 1 and then assigns the value of the increment to the pre. In other words, after this statement is executed, the value of the pre is 3,n 3.

As a result, if the + + predecessors, then the number of operations + + 1 First increase, and then participate in other operations; if the + + post, then the operands of + + will participate in other operations, then increase by 1. Strictly speaking, the value of the operand of the predecessor + + is increased by 1 before being used, and the value of the operand of the back + + is increased by 1 after being used. For example:

int n = 5, post = 1, pre = 1;
Pre = ++n + pre; //The pre is 7 after the end of the operation
n = 5;
Post = n++ + post; //The post is 6 after the end of the Operation

2. Self-subtraction operator (decrement Operator)

Self-subtraction operator--causes the operand value to be reduced by 1. --can be placed either in front of the operand or in the back. For example:

--n;
n--;

The self-subtraction operator is very similar to the self-increasing operator, except that the decrement operator causes the operand to be reduced by 1, while the self-increasing operator increases the operand by 1. For example:

int n = 5, post = 1, pre = 1;
Pre =--n + pre; //The pre is 5 after the end of the Operation
n = 5;
Post = n--+ post; //The post is 6 after the end of the Operation

3. Priority level

The self-add and decrement operators have high precedence, and only the parentheses have higher precedence than them. therefore, n*m++; means n (m++); instead of (n * m) + +;. and (n * m) + +; Is wrong. Because the operands of + + and--can only be variable left values (modifiable lvalue), and N * m is not.

Note that you should not confuse precedence with the order of values. For example:

int x = 1, y = 2, z;

Z = (x + y++) * 3; //After the end of the Operation Z is 9,y 3

To replace the above statement with a number:

Z = (1 + 2) * 3;

Y will increase by 1 only when the value of Y is used. The priority indicates that + + is used only for Y, not (x + y). The priority also indicates when the value of Y is used, but when the value of Y is increased by 1 is determined by the nature of the self-increasing operator.

When y++ is part of an arithmetic expression, you can assume that it means "use the value of y first, then increment." Similarly, ++y represents "self increase first, then use the value after the increment".

==========================================================================

The following is from the "C Language FAQ" original book: Steve Summit Translation: Zhu Qun, Sun Yun

Http://c-faq-chn.sourceforge.net/ccfaq/index.html

Http://www.eskimo.com/~scs/C-faq/top.html

==========================================================================

4.3 for code int i = 3; i = i++; Different compilers give different results, some are 3, some are 4, which is correct?

There is no correct answer; This expression has no definition. See also question 3.1, 3.7 and 11.32. Also note that i++ and ++i are different from i+1. If you want to make I self 1, use i=i+1, i+=1, i++ or ++i, not any combination, see question 3.10.

i = i++ 's behavior is undefined, but I just tested it on an ANSI-compliant compiler and got the results I wanted.

In the face of undefined behavior, including the scope of the implementation definition behavior and uncertainty behavior, the compiler can do any implementation, including all your expected results. But it is unwise to rely on this realization. Attend questions 7.4, 11.31, 11.32 and 11.34.

4.2 using my compiler, the following code int i=7; printf ("%d\n", i++ * i++); Return 49? In any order, shouldn't you print out 56?

Although the suffix self plus and suffix decrement operator + + and the -- output of its old values will not perform operations, but here the ' after ' is often misunderstood. there is no guarantee that the self-increase or decrement occurs immediately after the output variable's original value and before the other parts of the expression are evaluated. It is also not guaranteed that the update of a variable will occur at a time before the expression ' complete ' (in terms of ANSI C, before the next ' sequence point ', see problem 3.7). In this example, the compiler chooses to multiply the old values of the variables and then add them back to their own operation.

The behavior of code that contains multiple, indeterminate side effects is always considered undefined. (In simple terms, "multiple indeterminate side effects" refers to any combination of self-subtraction and assignment operators that are used in the same expression to cause the same object to be modified two times or modified and then referenced.) This is a rough definition; The strict definition See question 3.7, ' undefined ' meaning see question 11.32. Don't even try to find out how these things are implemented in your compiler (this is the opposite of many of the C textbook-retarded exercises); As K&r wisely pointed out, "If you do not know how they are implemented on different machines , such ignorance may just help protect you."

4.7 How can I understand complex expressions? What is the ' sequence point '?

A sequence point is a point in time (after the entire expression has been calculated or at the | |, &&,?: or comma operator, or before the function call), and the dust settles and all side effects are ensured to end. Ansi/iso C Standard is described in this way:

between the previous and next sequence points, the value saved by an object can only be modified once by the expression's calculation. And the previous value can only be used to determine the value that will be saved.

The second sentence is more puzzling. It says that if an object needs to be written in an expression, access to that object in the same expression should be limited to directly evaluating the value that will be written. This rule effectively limits the validity of an expression that only ensures that the variable is accessed before it is modified. For example i = i+1 is valid, while a[i] = i++ is illegal (see problem 3.1).

See the question below 3.8.

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.