Environment: win7
IDE: DEV-C ++
Compiler: GCC
1. Let's talk about the basics of ++ I and I ++.
The Code is as follows:
#include <stdio.h>//just change simplevoid stop(void){ system("pause"); }int main(void){ int i = 1; printf("i++ = %d\n",i++); printf("i = %d\n",i); int j = 1; printf("++j = %d\n",++j); printf("j = %d\n",j); printf("i++ = : %d ++i = %d\n",i++,++i); printf("i = %d\n",i); printf("++j = : %d j++ = %d\n",++j,j++); printf("j = %d\n",j); stop(); return 0;}
Running result:
i++ = i = ++j = j = i++ = : ++i = i = ++j = : j++ = j =
1) only when I ++ encounters a semicolon (;) will it affect the value of I. The output value of I ++ is still 1, and the value of I is also 1.
2) After I passes the semicolon, I = 2 because of auto-incrementing
3) ++ j will affect the j value and ++ j value regardless of the Semicolon ";", so J = 2
4) The value of j is also 2.
5) depending on the compiler and the operating system, the printf computing direction is also different. This is calculated from the right, first ++ I, then I ++, so output 3 and 3.
6) The last I ++ encountered a semicolon, So I = 4
7) from the right, the result printed by j is still 2, but the value obtained by the second operation is 3, so ++ j = 4
2. Clarify the priority of * And ++ in * p ++.
#include <stdio.h> stop( system( main( i = * p = & printf( v = *p++ printf( printf( printf( system( }
Running result:
-p = v = -p = i =
From the result, the ++ symbol affects the value of p and does not affect the value of I. It seems that the priority of ++ is higher than the pointer * symbol.
Let's look at the example.
#include <stdio.h> stop( system( main( i = * p = & printf( v = ++* printf( printf( printf( system( }
Calculation Result:
-p = v = -p = i =
How can ++ not affect the value of p?
Let's look at the example.
Slightly adjust the code: v = * ++ p;
Calculation Result:
-p = v = -p = i =
Ah, it turns out that * And ++ have the same priority level. The same level is calculated from right to left.
OK. All problems are solved gradually,
* (P ++)
* (++ P)
(* P) ++
++ (* P) should be okay.
Fog finally opened up...