Static and extern, staticextern

Source: Internet
Author: User

Static and extern, staticextern

Static and extern modifier local variables

Static modifier local variables:

Purpose: 1) the scope of static variables will be extended.

2) statements defining static variables are executed only once.

Extern cannot modify local variables

Void test (int num) {static int n = 0; printf ("% d", n); n ++;} int main () {test (); // The output result is 0 test (); // The output result is 1 test (); // The output result is 2}

Static effect on global variables (internal variables)

Static global variable, indicating that the global variable can only be used in the current file, but not in other files

Variables with the same name can be declared using static in different files

Effect of extern on global variables

The global variable modified by extern indicates that the current variable can be used in this file, or

Use other files

The global variables declared by extern cannot have the same name in different files (the two files must have an inclusion relationship)

Functions of static and extern

The static modifier is an internal function (which can only be used in the defined file)

The function declared by extern can be used in the definition file or in other files.

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.