C language A detailed description of the place "prevent improper use and error"

Source: Internet
Author: User

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"

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.