Example of static keywords in C language-local static variables

Source: Internet
Author: User

In C, the static keyword can be used in the global range, function range, or local range! First run the experiment code: [cpp] # include <stdio. h> void func (int I) {if (I & 0x01) = 1) {static int j = 1; // static variable j ++ defined in the local range; printf ("I = % d, j = % d. \ n ", I, j) ;}else {static int j = 0; // define another static variable j ++; printf (" \ ti = % d, j = % d. \ n ", I, j);} // printf (" j = ", j); // error. variable j is not defined return;} int main () {for (int I = 0; I <10; ++ I) func (I); return 0;} the code output is as follows: the execution result shows that the static variable j defined in if is independent from the static variable j defined in else, and the "Last value assignment result" is retained, stat defined in if and else Ic variables are visible only in the if or else local range, but not in the local range.

Related Article

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.