Static and extern keywords in C Language

Source: Internet
Author: User

InC LanguageStatic (static) and extern (external) keywords are often used, but improper use mayProgramThe design is troublesome. Here we will introduce it.

1. Declare or define variables

1. Local variables:

Static variables defined in the function are static local variables stored in the static/Global Area of the memory. The life cycle of the variables is the entire source program. However, the scope is the same as that of the automatic variable and can only be used in this function. After exiting the function, this variable continues to exist but is unavailable. If you do not assign a value to a static local variable, the system automatically assigns a value of 0 (the initial value of the automatic variable is displayed immediately ).

2. Global Variables

Global variables are extern by default. Static global variables are used if they are defined. If you do not assign an initial value to a global variable, the system automatically assigns 0 values.

(1) The extern global variable can be called by functions in other source files, but it must be declared again with extern in the called file, or the global variable can be directly put in the header file, include this header file in the file that needs to be called.

(2) Static global variables. The life cycle is the entire source program, which is stored in the static/Global zone, but can only be used in the source file where it is located.

2. Declare or define functions

1. Static Functions

The lifecycle of a static function is also the entire source program, but the scope is also the source file.

2. extern Functions

The default function type is extern. When you need to call an extern function in other files, you need to declare this function with extern before use, or include the header file that declares this function. When declaring an external function in the. c file, the extern keyword must be used, while the extern keyword in the header file can be dispensable, as long as it is implemented in a. c file.

During programming, declare the global variables and functions that need to be used in other files in the header file. Include this header file in use. Do not define variables and functions in the header file.

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.