C-functions and variables

Source: Internet
Author: User

7.4 internal and external variables

All the variables in C language have their own scopes. Variables indicate different locations and have different scopes. Accordingly, variables in C language are divided into internal variables and external variables.

7.4.1 internal variables

7.4.2 external variables

7.4.1 internal variables

The variable described in a function is an internal variable, which is only valid within the function range.

That is to say, the described variables can be used only within the function that contains the description of variables. These variables cannot be used outside the function. Therefore, internal variables are also called "local variables ".

For example:

Int F1 (int A)/* function F1 */

{Int B, C;

......

}/* A, B, and C scopes: only in function F1 */

Int F2 (int x)/* function F2 */

{Int y, z;

......

}/* X, y, and z scopes: limited to function F2 */

Main ()

{Int M, N;

......

}/* M, n scope: only available in main */

The scope of local variables should also be described as follows:

1. The internal variables defined in main () of the main function can only be used in the main function. Other functions cannot be used. In addition, internal variables defined in other functions cannot be used in the main function. Because the main function is also a function, it is in parallel with other functions. This is different from other languages and should be noted.

2. The parameter variable is also an internal variable, which belongs to the called function. The real variable is an internal variable that calls the function.

3. The same variable names can be used in different functions. They represent different objects, and different units are allocated without interference or confusion.

4. variables can also be defined in composite statements, and their scope is only within the scope of composite statements.

7.4.2 external variables

Variables defined outside the function are called external variables. Similarly, an array defined externally in a function is called an external array.

An external variable does not belong to any function. Its scope is from the position defined by the external variable to the end of this file.

External variables can be directly referenced by all functions in the scope. Therefore, external variables are also called global variables.

[Case 7.9] enter the length (L), width (w), and height (H) of the cube to calculate the cube size and area of the positive, side, and top.

/* Case code file name: al7_9.c */

/* Function: Use global variables to calculate the volume of the cube and the area of the three faces */

Int S1, S2, S3;

Int vs (int A, int B, int C)

{Int V;

V = a * B * C; S1 = a * B; S2 = B * C; S3 = A * C;

Return V;

}

Main ()

{Int V, L, W, h;

Clrscr ();

Printf ("/ninput length, width and height :");

Scanf ("% d", & L, & W, & H );

V = vs (L, W, H );

Printf ("V = % d S1 = % d S2 = % d S3 = % d/N", V, S1, S2, S3 );

Getch ();

}

Global variables are described as follows:

(1) external variables can enhance the data connection between function modules, but make these functions depend on these external variables, thus reducing the independence of these functions.

From the modular program design point of view, this is unfavorable, so do not use external variables when it is not unavailable.

(2) allow external variables and internal variables to have the same name in the same source file. Within the scope of the internal variable, the external variable is blocked and does not work.

(3) The scope of external variables is from the definition point to the end of this file. If the function before defining a vertex needs to reference these external variables, you must describe the referenced external variables in the function. The external variables are described as follows:

Extern data type external variable [, external variable 2…];

Note: definitions of external variables and descriptions of external variables are two different things. External variables must be defined only once outside all functions. The description of the external variable appears in the function to use the external variable, and can appear multiple times.

[Case 7.10] definitions and descriptions of external variables.

/* Case code file name: al7_10.c */

Int vs (int xl, int XW)

{Extern int XH;/* external variable XH Description */

Int V;

V = xl * XW * XH;/* directly use the value of the external variable XH */

Return V;

}

Main ()

{Extern int XW, XH;/* external variable Description */

Int XL = 5;/* internal variable definition */

Printf ("XL = % d, XW = % d, XH = % d/nV = % d", XL, XW, XH, vs (XL, XW ));

}

Int XL = 3, XW = 4, XH = 5;/* Definitions of external variables XL, XW, and XH */

7.5 internal and external functions

When a source program is composed of multiple source files, the C language divides the function into internal functions and external functions based on whether the function can be called by functions in other source files.

7.5.1 internal functions (also known as static functions)

7.5.2 external functions

7.5.3 compilation and connection of multiple source program files

7.5.1 internal functions (also known as static functions)

A function defined in a source file can only be called by functions in this file, but cannot be called by functions in other files of the same program. This function is called an internal function.

To define an internal function, you only need to add a "static" keyword before the function type, as shown below:

Static function name (function parameter table)

{......}

The keyword "static" is "static" in Chinese. Therefore, internal functions are also called static functions. However, the meaning of "static" here is not the storage method, but the scope of the function is limited to this file.

The advantage of using internal functions is that when different people write different functions, you don't have to worry about whether your own defined functions will have the same name as the functions in other files, because the same name does not matter.

7.5.2 external functions

External Function Definition: when defining a function, if the keyword "static" is not added or the keyword "extern" is appended, this function is an external function:

[Extern] function name (function parameter table)

{......}

When calling an external function, you need to describe it:

[Extern] function name of the function type (parameter type table) [, function name 2 (parameter type table 2)…];

[Case 7.11] external function application.

(1) file mainf. c

Main ()

{Extern void input (...), Process (...), Output (...);

Input (...); Process (...); Output (...);

}

(2) file subf1.c

......

Extern void input (......) /* Define external functions */

{......}

(3) file subf2.c

......

Extern void process (......) /* Define external functions */

{......}

(4) file subf3.c

......

Extern void output (......) /* Define external functions */

{......}

7.5.3 compilation and connection of multiple source program files

(1) General Process

Edit the source files → create a project file → set the project name → compile, connect, run, and view the results.

(2) create a project file

Create a file extension with the same method as editing the source file. prj project file: This file only contains the names of the source files to be compiled and connected, one row, and the extension. C can be set by default. The order of file names only affects the compilation sequence and is irrelevant to running.

Note: if one or more of the source files is not in the current directory, You Should prefix the file name with the path.

(3) set the project name

Open the menu, select project/project name, and enter the project file name.

(4) Compile, connect, run, and view results

Same as a single source file. The main file names are the same as the main file names of the project files.

Note: After debugging the current project file, select project/clear project and clear the project name from "project name" (empty after clearing ). Otherwise, the project file is always compiled, connected, and run!

(5) Error Tracking

It only tracks the current source program file. If you want to automatically track all source files in the project, set the options/environment/message tracking switch to "all files": press the Enter key continuously until "all files" appears. When an error message is rolled in the message window, the system automatically loads the corresponding source file to the edit window.

You can also disable tracing (Set "message tracking" to "off "). At this time, as long as you locate the error information you are interested in, and then press enter, the system will automatically load the corresponding source file to the editing window.

7.6 introduction to dynamic and static storage of Variables

In C language, there are four types of storage variables: auto, register, extern, and static ). Automatic variables and register variables are stored dynamically, while external variables and static internal variables are stored statically.

7.6.1 storage of internal variables

7.6.2 storage of external variables

7.6.1 storage of internal variables

1. static storage-static internal variables

(1) Definition Format: static data type internal variable table;

(2) storage features

1) static internal variables belong to static storage. During program execution, the function is not released even after the function call is completed. In other words, static internal variables always exist during program execution, but other functions cannot reference them.

2) If it is defined but not initialized, it is automatically assigned "0" (integer and real) or '/0' (struct type). Each time you call the function where they are located, do not re-assign the initial value, just keep the value at the end of the last call!

(3) When to use static internal variables

1) the value at the end of the last call of the function needs to be retained.

2) variables are referenced without changing their values.

2. Dynamic Storage-Automatic local variables (also called automatic variables)

(1) Definition Format: [auto] Data Type Variable table;

(2) storage features

1) Automatic variables are stored dynamically. The automatic variables defined in the function are valid only in the function. When the function is called, the bucket is allocated and the call ends.

The automatic variables defined in the composite statement are valid only in the composite statement. After exiting the composite statement, they cannot be used. Otherwise, an error occurs.

2) definition without initialization, its value is uncertain. If Initialization is performed, the initial value assignment operation is performed at the time of the call, and the initial value must be assigned again for each call.

3) because the scope and lifetime of automatic variables are limited to the individual defining them (functions or composite statements), variables of the same name can be used in different individuals without confusion. Even the automatic variables defined in the function can have the same name as the automatic variables defined in the compound statement within the function.

Suggestion: the system will not be confused, it does not mean that people will not be confused, so try to use as few automatic variables with the same name!

[Case 7.13] storage features of automatic variables and static local variables.

/* Case code file name: al7_13.c */

Void auto_static (void)

{Int var_auto = 0;/* automatic variable: reinitialize each call */

Static int var_static = 0;/* Static local variable: Only initialize once */

Printf ("var_auto = % d, var_static = % d/N", var_auto, var_static );

++ Var_auto;

++ Var_static;

}

Main ()

{Int I;

For (I = 0; I <5; I ++) auto_static ();

}

3. Register storage-register variable

Generally, variable values are stored in the memory. To improve execution efficiency, the C language allows you to store the values of local variables in registers, which are called register variables. The definition format is as follows:

Register data type variable table;

(1) only local variables can be defined as register variables, that is, global variables do not work.

(2) The actual processing of register variables varies with the system. For example, MSC and TC on a microcomputer actually process register variables as automatic variables.

(3) The number of registers allowed to be used is limited. No number of register variables can be defined.

7.6.2 storage of external variables

External variables are stored in static mode:

(1) Static external variables -- can only be referenced by functions in the source file

The definition format is static data type external variable table;

(2) non-static external variables-allow reference by functions in other source files

When defined, the external variable with the default static keyword is non-static. For functions in other source files that reference non-static external variables, you must describe them in the source file where the referenced function is located:

Extern data type external variable table;

Note: The description of the extern variable in the function indicates that the external variable in the source file is referenced! Description of the extern variable outside the function (usually at the beginning of the file), indicating that the external variable in other files is referenced.

Static local variables and static external variables belong to the same static storage method, but the two have major differences:

(1) different positions are defined. Static local variables are defined in the function, and static external variables are defined outside the function.

(2) different scopes. Static local variables belong to internal variables, and their scopes are limited to those defined in their functions. Although the lifetime is the entire source program, other functions cannot use them.

Static external variables are defined outside the function, and their scope is to define its source file; the lifetime is the entire source program, but functions in other source files cannot use it.

(3) the initialization process is different. Static local variables are initialized only when the function where it is called for 1st times. When the function that defines it is called again, the value at the end of the last call is not initialized. Static external variables are defined outside the function, and there is no "repeated" initialization problem of static internal variables. The current value is determined by the operation that assigned a value to them the last time.

Remember: After changing a local variable to a static internal variable, it changes its storage mode, that is, its survival time. After changing an external variable to a static external variable, it changes its scope and limits its scope of use. Therefore, the keyword "static" has different roles in different places.
 

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.