2. Declaration and definition of variables & intrinsic functions and external functions

Source: Internet
Author: User

Declaration and definition of "3" variable

for functions. The declaration part declares the property of the identifier (variable, function, struct), and the function is the prototype of the function, and the definition of the function is the definition of the function. The declaration of the modulated function is placed in the declaration part of the keynote function, and the definition of the function is clearly not within the scope of the declaration part, it is a standalone module!

For variables. There are two cases of variables that occur in the Declaration section: (int A;), which does not require a resume storage space (extern A;) The former is called a definition declaration; the latter is called a citation statement;

"4" intrinsics and external functions

Some functions can only be called by other functions in this file, cannot be called by functions in other files, others can only be called by other functions in this file and cannot be called by other files.

The function is distinguished as an intrinsic function and an external function, depending on whether the function can be called by other source files;

   An intrinsic function, also called a static function , is declared with static, using an intrinsic function to limit the function to the file in which it resides, so that even if there are internal functions with the same name in different files, they do not interfere with each other .

  an external function , declared with extern, indicates that the function is an external function defined in another file .

Functions are inherently external, and in a program it is often necessary to invoke external functions in other files, for easy programming, the C language allows to write extern when declaring a function. A function prototype allows you to extend the scope of a function beyond the file that defines the function, as long as the function prototype of the function is included in each file that uses the function. function prototype notifies the compilation system that the function is defined later in this file, or in another file .

Common ways to scope functional prototype extension functions #include,include The specified header file contains the information required to call the library function.

——————————————————————————————————————

Global static variables

Add the keyword static before the global variable
1) in-memory location: static storage (static storage is present during the entire program run)
2) Initialize: Uninitialized global static variable is automatically initialized to 0 by the program (the value of the automatic object is arbitrary unless he is displayed initialized)
3) Scope: Global static variables are not visible outside the declaration of his file. Start with the definition exactly to the end of the file.

Benefits:
<1> will not be accessed and modified by other files. The static global variable is only initialized once, preventing it from being referenced in other file units ;
<2> other files can use variables of the same name, and no conflicts will occur.

Local static variables
Add the keyword static before the local variable
1) in-memory location: Static Storage Area
2) Initialize: Uninitialized global static variable is automatically initialized to 0 by the program (the value of the automatic object is arbitrary unless he is displayed initialized)
3) Scope: The scope is still a local scope, and when the function or statement block defining it ends, the scope ends.

Note: When static is used to modify a local variable, it changes the location where the local variable is stored and changes from the original stack to a static storage area. But the local static variable is not destroyed after it leaves the scope , but still resides in memory until the program is finished, but we can no longer access it.
When static is used to modify a global variable, it changes the scope of the global variable (which is invisible outside of declaring his file), but does not change its location or in the static store.

static functions

  The definition and declaration of a function is extern by default, but the static function is only visible in the file that declares him and cannot be used by other files.

Benefits of defining static functions:
<1> other files can define the same name function, no conflict
<2> static functions cannot be used by other files.

Storage descriptor auto,register,extern,static, corresponding to two storage periods: automatic storage period and static storage period. Auto and register correspond to automatic storage periods. A variable with an automatic storage period is established when it enters the block that declares the variable, it exists when the block is active, and is revoked when it exits the block.

<3> Static functions are automatically assigned to a storage area that has been in use until the application instance is exited, avoiding the pressure stack out of the calling function , much faster

What is the difference between a static function and a normal function: The static function hasonly one copy in memory, and the normal function maintains a copy of each call

2. Declaration and definition of variables & intrinsic functions and external functions

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.