The relationship between external variables and functions in C program

Source: Internet
Author: User
Tags definition

A C program consists of a set of objects that include variables used in programs and functions that implement specific functions. Variables can be divided into function internal definitions, variables and functions that are defined externally are commonly defined as internal or local variables, and variables that are defined outside the function, which are used by many functions, are called external variables, and are generally referred to as global variables.

Because the C language does not allow other functions to be defined in one function, the function itself is external. In general, you can also say that a function is a global function.

By default, external variables and functions have the following properties: All references to external variables and functions by name (even if the reference comes from independently compiled functions) are the same object referenced (this property is referred to as an outer join in the standard).

Because external variables are globally accessible, this provides a way to replace a function variable to return a value in order to exchange data between functions. Any function can use a name to access an external variable, as long as the name has been described or defined somewhere.

If you want to share a large number of variables between functions, it is more convenient and efficient to use external variables than to use a long argument table. However, it can also cause programs to generate too many data connections between functions.

The purpose of external variables is also expressed in that they have greater scope and longer lifetimes than internal variables. An internal automatic variable can only be used inside a function, when its function is invoked, and it disappears when the function exits. The external variables are permanent, and their values remain unchanged from one function call to the next. Therefore, if two functions must share some data, and the two functions do not call each other, it is most convenient to use the shared data as an external variable instead of being passed as a variable.

1, the definition and use of external variables

According to the C language standard, external variables can only be defined once in all source files of the program, or they can result in duplicate-defined compilation errors.

1.1 Definition and declaration of external variables

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.