Differences between static keyword usage and dynamic variable

Source: Internet
Author: User

Why do I use static variables?

There are two ways to store variables: Static and Dynamic. dynamic variables are assigned storage units during program execution. When the program segment ends, these storage units are automatically released, static variables are stored in permanent storage units. The original storage unit is still used when the program segment is executed the next time.

Therefore, the life cycle of static variables will be extended until the end of the entire program running.
Test code:
# Include <stdio. h>
Void sum ();
Void main ()
{
Int a = 3;
Static int B;
Printf ("a = % d, B = % d \ n", a, B );
Sum ();
Sum ();
}
Void sum ()
{
Int a = 2;
Static int B = 10;
A + = 10;
B + = 20;
Printf ("a = % d, B = % d \ n", a, B );
}

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.