1. Run the following code:
#include <stdio.h> #include <string.h>int main () { int A; a=1; int s[4]; memset (s, 0, sizeof (s)); s[a++]=a++; printf ("s[0]=%d\n", S[0]); printf ("s[1]=%d\n", S[1]); printf ("s[2]=%d\n", s[2]); printf ("s[3]=%d\n", S[3]); printf ("A =%d\n\n\n", a); A=1; memset (s, 0, sizeof (s)); S[a++]=++a;
printf ("s[0]=%d\n", S[0]); printf ("s[1]=%d\n", S[1]); printf ("s[2]=%d\n", s[2]); printf ("s[3]=%d\n", S[3]); printf ("A =%d\n\n\n", a); A=1; memset (s, 0, sizeof (s)); s[++a]=a++; printf ("s[0]=%d\n", S[0]); printf ("s[1]=%d\n", S[1]); printf ("s[2]=%d\n", s[2]); printf ("s[3]=%d\n", S[3]); printf ("A =%d\n\n\n", a); A=1; memset (s, 0, sizeof (s)); S[++a]=++a; printf ("s[0]=%d\n", S[0]); printf ("s[1]=%d\n", S[1]); printf ("s[2]=%d\n", s[2]); printf ("s[3]=%d\n", S[3]); printf ("A =%d\n\n", a); return 0;}
The output is:
S[0]=0
S[1]=1
S[2]=0
S[3]=0
A = 3
Description: A=1; s[a++]=a++;
a++ will first use the value of a and then perform the calculation. First, such an assignment is s[1]=1; And then performed two a++ operations, so a=3.
S[0]=0
S[1]=0
s[2]=2
S[3]=0
A = 3
Description: A=1; S[a++]=++a;
First ++a, a=2. Then the s[2]=2 was carried out; One more a++, and finally a a=3.
S[0]=0
S[1]=0
s[2]=2
S[3]=0
A = 3
Description: A=1; s[++a]=a++;
First ++a, a=2. Then carry on the s[2]=2; Finally carries on the a++, finally a=3;
S[0]=0
S[1]=0
S[2]=0
S[3]=3
A = 3
Note: The first two times ++a, A=3; Then carry on the s[3]=3;
Summary: In the above-mentioned formula with increment operation, before the assignment operation, all the ++x is done, and the value of the variable is saved. The assignment operation is in progress.
After the assignment operation is done, the increment operation of x + + is performed. This will explain all the running results above.
(PS: You can use the above code to run the results to examine a person's mastery of C-language operations Ah!) )
C language A detailed description of the place "prevent improper use and error"