Local variables, the application of global variables

Source: Internet
Author: User

#include <stdio.h>

int count=3;

int main ()

{

int i,sum,count=2;

for (i=0,sum=0;i<count;i+=2,count++)

{

static int count=4;

count++;

if (i%2==0)

{

extern int count;

count++;

Sum+=count;

}

Sum+=count;

}

printf ("sum=%d\ncount=%d\n", Sum,count);

return 0;

}


I<count in count=2; Enter for loop, static int count=4 count is a static local variable, after the current value is saved after use, count++, when Count becomes 5, enter the IF statement, extern int count, at which point the count is the outer global variable, cou nt++; When Count becomes 4, the sum+=count;sum=4 is executed. Out if statement, execute Sum+=count, at this time sum=4+5=9. Enter the adjustment portion of the For Loop, i+=2,count++, at which point the i=2,count=3 is executed, the judgment part of the For loop executes, 2<3 is established, enters for loop, continues execution, count++, count=6 at this time, enters if statement, count++, At this point count is added 1 for the last value, so count is 5,sum+=count, then sum=9+5=14, then the IF statement, Sum+=count, then sum=14+6=20, and then into the adjustment portion of the For Loop, i+=2,count++, at which time i= 4,count=4; conditional part 4<4 judgment fails, out for loop, output sum=20,count=4. The program ends.



Local variables, the application of global variables

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.