Absrtact: In C language teaching, the self-increment self-subtraction operator is widely used, and it has certain skill and difficulty. Among the many operators, the self-increment decrement operator is the most difficult to understand and the most error-prone operator. This paper probes into the function and application of the self-increment self-decrement operator in order to help improve the teaching effect of C language.
Chinese paper net http://www.xzbu.com/8/view-3865031.htm
Keywords: c language; self-increment self-subtraction operator; teaching effect
Middle Image Classification Number: G434 Document ID: A article ID: 1672-7800 (2012) 012-0188-02
0 Introduction
In C programming design, if the self-increment self-decrement operator is used rationally, it can save code and improve efficiency, if it is not understood and mastered correctly, it is apt to cause mistake, which often makes the students very puzzled when they are beginners. In teaching, it is necessary to analyze it carefully so that students understand and master the methods and techniques of using the self-increment operator.
1 basic knowledge of the self-increment decrement operator
The C language Program provides two special operators for variable increment and decrement. An operator that increments the value of a single variable by 1 is called the self-increment operator, denoted by "+ +", and the operator that reduces the value of a single variable by 1 is called the self-decrement operator, denoted by "--". The self-increment decrement operator is the monocular operator, which can only be applied to an object, and the result is still assigned to the object, that is, the result is stored in the storage unit of the original variable. For example, int i=2; i++; After i++ is executed, i=3. At this point, the value stored in the original variable i storage unit becomes 3. Here i++ is equivalent to i=i+1, of course, i--is equivalent to i=i-1.
The operand of the self-increment decrement operator can only be a variable, not a constant or an expression. As long as the object of its operation is a standard variable, whether it is an integer, a real type or an enumerated type can be used as an operand, and the constants and expressions do not have a storage unit, it is natural not to do self-increment reduction operations. For example: ++n, i--Legal, and ++10, (i+j) + + illegal. Because if the constant, the value of the constant cannot be changed, can not be added 1, if the expression, such as (I+J) + +, if i+j=5, the value of the self-increment is 6 of the variable space to be stored, it is an illegal expression.
The self-increment decrement operator consists of two cases: front-mounted (++i,--i) and back-mounted (i++,i--). The meanings of the pre-and post-position are: ++i: The variable i increment by 1, and then use the value of the variable i, I: The variable i 1, and then use the value of the variable i, i++: First use the value of the variable I, and then the variable i 1;i--: First use the value of the variable i, then the variable i minus 1. That is, the predecessor operation is "first change after use", the latter operation is "first use and then change". When the self-increment self-decrement expression appears as a single statement, there is no difference between the two, but there is a significant difference between referencing them in an expression. For example: {int i=9; x=i++; y=i;} The result of the execution is x=9,y=10, and the post operation is "first use and then change". The execution result of {int i=9; x=++i; y=i;} is x=10,y=10, that is, the predecessor operation is "first changed and then used". Therefore, the self-increment decrement operator is placed before or after the variable, and its operation order is different, and the result of the operation is different.
The self-increment decrement operator operates on the "right-to-left" binding, with only one operand, which is a single-mesh operator with a priority of 2, higher than the priority of the other binocular operators and the three-mesh operator. For example:
int main ()
{int i=10,j;
j=-i--;
printf ("i=%d,j=%d", I,j);}
In the example j=-i--is an expression, does the program execute j= (-i)-, or do j=-(i--)? The decrement operator "--" and the inverse Operator "-" are all single-mesh operators, with a priority of 2 and a combination of direction from right to left. The C language stipulates that the operation order of the same priority operator is determined by the binding direction, while the two operators are right-associative, so the expression executes j=-(i--) operation, the result is: i=9,j=-10.
2 application of the self-increment decrement operator
When students learn the self-increment operator, the key is to know that the predecessor operator is the "change first", and the latter operator is the "first use and then change" Operation rule.
2.1 Application in an assignment statement
Assignment statement, the self-increment self-decrement operation characters as the predecessor, its operation priority is higher than other arithmetic operation, namely the self-increment self-reduction operation according to the "right to left" binding, then carries on the other operation; if it is a post-set, its operation priority is lower than other arithmetic operations, that is, the other arithmetic operations, and then follow the " The combination of the self-increasing self-subtraction operation.
Example 1 analyzes the output after running the following program segment.
int main ()
{inti,p;
i=5; p= (i++) + (i++) + (i++); printf ("p=%d,i=%d\\n", p,i);
i=5; p= (++i) + (++i) + (++i); printf ("p=%d,i=%d\\n", P,i);}
Analysis: In the 3rd line statement expression p= (i++) + (i++) + (i++), the self-increment operation as a post-type, its operation priority is lower than other arithmetic operations. Therefore, the variable I itself first participates in the arithmetic operation, namely p=5+5+5=15, then carries on the self-increment operation according to "from right to left" the union, namely i++;i++;i++; the last I value is 8. Note that the 3rd line statement expression p= (i++) + (i++) + (i++) theoretically parses the value to be 5+6+7, while the actual results of the various compiler executions are 5+5+5. This means that the post-increment operator "Change first" refers to a uniform change before the next statement is executed, rather than just using it. The line statement is equivalent to p=i+i+i;i=i+1;i=i+1;i=i+1. The result of the 4th Line statement is related to the compiler, in the VC environment Expression p= (++i) + (++i) + (++i), equivalent to P= (((++i) + (++i)) + (++i)), the computer in the calculation of the shape such as p= (A+B) + (c+d) + (E+F), the first calculation (A+B) + (C+d), and store the results (for example: stored in P), and then calculate p+ (e+f) =p. Therefore, the original statement is equivalent to I=i+1;i=i+1;p=i+i;i=i+1;p=p+i, that is, p=7+7+8=22, and the value of the last I is 8. However, in the TC environment, the increment operation in the expression p= (++i) + (++i) + (++i) as the predecessor, its operation priority is higher than other arithmetic operations. Therefore, according to "from right to left" of the binding of the self-increment operation, that is, the value of I++;i++;i++;i to 8, and then the addition operation, that is, p=8+8+8=24. The difference in the results above is due to the fact that a statement in a high-level language is compiled and interpreted into several machine instructions, and the order of execution of several machine instructions ultimately determines the execution result of the equivalent statement, and the sequence of operations performed by the two compilation environments is different. 2.2 Applications in Logical expressions
Example 2 analyzes the output after running the following program segment.
int main ()
{inti=8,j=9,k=10,p;
p=i++| | ++j&&--k;
printf ("p=%d,i=%d,j=%d,k=%d,\\n", P,i,j,k);}
According to the rules of operation, the priority of the self-increment decrement operator is higher than that of the logical operator, and the i++, ++j 、--K should be computed first, and the output should be p=1,i=9,j=10,k=9 in theory. But in fact, in the vc++6.0 environment execution result is p=1,i=9,j=9,k=10, only the value of the variable i is changed, and the value of the variable J and K has not changed. Because the C language performs special operations on logical operators, short-circuit characteristics: In a complex logical expression, if the value of the front-face expression determines the value of the entire expression, subsequent sub-expressions are no longer scanned for calculations. Such an operation setting can speed up the execution of logical expressions. The value of i++ in the program is 8, the result is true (1), and the logical operator is executed first or (| | Operation, because the previous is true, regardless of whether the logical value of the following expression is true or false, the result is true, so ++j 、--K is not executed.
2.3 Applications in the cyclic structure
There are 3 loop structure statements in the C language: The while statement, the Do While statement, and the for statement. The first two statements determine whether to loop based on conditions, and the latter statement executes the loop based on the number of times it is set. No matter what kind of loop, there must be a statement that tends to end the loop, which is often implemented by i++,i--, which I call a cyclic variable. Changing the value of the loop variable in the loop body eventually causes the loop condition to not be established, thus ending the loop. The general for statement uses more, for example: for (i=1;i<=100;i++) s=s+i; when the loop variable i=101, the condition is not set and the loop ends.
2.4 Application in expression of function arguments
If the self-increment decrement operator is used for a function argument expression, its calculation method is completely different from the result of the operation as a normal statement. In the VC environment, the C language function execution process is to put the value of the function argument expression into the stack from right to left, the value of the function argument expression is calculated before the stack, and the function is removed from the stack to use the parameter value. Therefore, if the function has more than one parameter, they are evaluated in the right-to-left order.
Example 3 analyzes the output after running the following program segment.
int main ()
{int i=5;
printf ("%d,%d,%d\\n", I,++i,--i);}
The function printf () has 3 parameters, evaluates the parameters in right-to-left order: first computes the value of-I, computes the value of the ++i, calculates the I value, and the result is output from left to right as 5,5,4.
2.5 application in the pointer
In the C language, pointers are very flexible and easy to use, pointing to a variety of variables, to arrays, functions, and files. When the pointer points to a certain data object, it can either move down to the next object, or move up to the previous object, which is achieved by the self-increment decrement operator, whose main purpose is to fix the address. The C language stipulates that when referencing a pointer variable, it must be "first defined and then used". If you define a pointer variable that points to a certain data type, p++ points to the next element in memory, p--points to the previous element in memory instead of simply increasing the value by 1 or minus 1. The address that p++ represents is actually (p+1) Xd,d is the number of bytes that a data object occupies.
Example 4 analyze the output after running the following program.
int main ()
{int a[5]={1,2,3,4,5}; M
Abstract:
Key Words:
int *num[5]={&a[0],&a[1],&a[2],&a[3],&a[4]};
int **p,i;
P=num;
for (i=0;i<5;i++)
{printf ("%d", **p);
p++;}
printf ("\\n");}
The program defines p as a pointer variable pointing to the pointer data, starting at the first element of the pointer array num num[0], and num[0] is a pointer-type element that points to the first element of the shape array a a[0]. At the start, the value of P is &num[0],*p to num[0], which is the value &a[0],**p is a[0]. Therefore, the first output is the value of a[0] 1, and then execute p++,p refers to the next element num[1], not a[1], and then output **p, that is, the value of a[1] 3.
3 Conclusion
The above discusses the application of the self-increment decrement operator in many aspects of the programming statement, hoping to be helpful for understanding and using the self-increment decrement operator. There will be a lot of difficulties in the actual teaching, especially for the different compiling systems, which need to be operated on more machines and understood in practice. Of course, if these basic applications are mastered, other difficulties are relatively easy to solve. Through the analysis and discussion of the difficult knowledge in C language teaching, it will help to improve the effect of classroom teaching so as to lay a solid foundation for students to learn this important language course.
Reference documents:
[1] rectification. C program Design [M]. 4th edition. Beijing: Tsinghua University Press, 2010.
[2] He Chin, Genhui. C language programming [M]. Beijing: Higher Education Press, 2008.
[3] BRIAN W,kernighan,dennis M,et al. C language programming [M]. 2nd edition. Beijing: Machinery Industry Press, 2007.
[4] Summer is superior. The application of C-language self-increment-decrement operator [J]. Journal of Wuhan Institute of Engineering and Technology, 2010 (3).
[5] Wu Qiong. Analysis of the binding of C-language operators [J]. Computer knowledge and Technology, 2007 (2).
(Editor: Sun Juan)
Please indicate the source of the reprint. Original address: http://www.xzbu.com/8/view-3865031.htm
[To] the teaching research of self-increment-subtraction operator in C language-China paper net