Dark Horse Programmer--c Language Foundation--variables, intrinsic functions and external functions

Source: Internet
Author: User


------- iOS Training look forward to communicating with you! ----------


First, variables

A variable represents a named storage unit with a specific attribute, and the value of the variable can be changed during operation. variables must be defined first and then used. when defined, specifies the name and type of the variable. The variable name is actually a storage address represented by a name, and the value from the variable is actually found in the corresponding memory address by the variable name, and then the corresponding data is read from the storage unit.

We have just mentioned the term variable definition, and we know that when we define a function, we are going to make a declaration of the function, which in turn references a concept: declaration. So let's take a look at what declarations and definitions are: for example, declaring a variable means describing the type of the variable to the compiler, but not allocating storage space for the variable, but defining a variable means allocating storage space for the variable while declaring the variable. You can also initialize the variable while defining a variable.

We may define variables in the following 3 Scenarios:

(1) defined at the beginning of the function;

(2) In the compound statement within the function is defined;

(3) defined outside of the function

From the perspective of the scope of a variable, variables are usually divided into: local variables and global variables

(1) Local variables are usually defined only, and global variables are defined in the source file and declared in the header file;
(2) Local variables can be understood as: variables defined inside a function are internal variables, which are valid only within the scope of the function. Local variables of the same name can be used in different functions, which represent different objects and do not interfere with each other.

(3) Global variables can be understood as: variables defined outside the function is called an external variable, the external variable is a global variable, it can be shared with other functions in this file, global variables in the execution of the program all occupy the storage unit, which will reduce the speed of the program, and will also reduce the generality of the function and so on , so use as few global variables as possible.

c 4 auto Span style= "font-family: ' Times New Roman ';" >static

(1) automatic variable (auto variable): The local variable in the function, whose default format is the automatic variable type.

For example, in the body of a function

int B, c=3; auto int b, c=3;

The two-game code is actually equivalent.

Automatic variables are dynamically allocated storage space, which is released when the function is finished. If an automatic variable is not assigned an initial value, its values are an indeterminate value.

(2) static local variable (static local variable ): a local variable declared and defined in the body of a function that is used only by this function, that is, other functions cannot invoke it. The value of a static local variable does not disappear at the end of the function call and retains the original value, that is, the storage unit it occupies is not freed, and at the next function call, the variable already has a value, which is the value at the end of the last function call.

Static local variables allocate storage units in the static store and are not freed during the entire run of the program. Static local variables are assigned the initial value at compile time, that is, the initial value is assigned only once.

in SDT in the compiler, it is recommended to assign an initial value to a static local variable, otherwise the initial values of the static local variable are indeterminate. In other compilers, uninitialized static local variables may have an initial value of zero, which is determined by the specific compiler, preferably tested before use.

< Span style= "font-family: ' Times New Roman '; font-size:16px;" > (3 register variable): with

register c c Span style= "font-family: ' The song body '; font-size:16px;" > The compiler can make better decisions than programmers.

(4) external variable (extern global variable): When defining a global variable in a function, its default format is the external variable type. External variables should be declared in a header file and defined in the current source file. External variables allow other file references.

The following example declares a variable and a struct, defines two variables, one of which is initialized with:

extern int DECL1;       This is a declarationstruct decl2{int member;};   This just declares the type–no variable mentionedint def1 = 8;   This is a definitionint def2; This is a definition

Second, function

In essence, functions are used to accomplish certain functions, and a C Language program can consist of a main function and several other functions. And all the functions used in the program must be defined first, and then used, today the definition of the function and the knowledge of the use is not explained here, the main point is the internal function and the external function of the difference and use.

(1) Intrinsic functions: declarations and definitions of intrinsic functions are done in the current source file, while external functions are usually defined in the source file and declared in the header file.

Format:

Static type name Function name (formal parameter list); for example: static int fun (int a, int b);

Functions that are used only in the current source file should be described as intrinsic functions. Intrinsic functions should be declared and defined in the current source file. If an intrinsic function is declared in a header file, other source files can also use this function by including the header file, but this loses its meaning as an intrinsic function.

Pros: Using intrinsic functions, you can restrict the function to the file. This avoids collisions with functions of the same name that might occur in other source files.

Cases:

File:function1.c#include "function1.h" static int stat_func (void), void masterfunction (void) {... rc = Stat_func (); ...} static int Stat_func (void) {... return RC;}

(2) External functions

Format:

extern type name Function name (formal parameter list); for example: extern int fun (int a, int b);

For functions that can be used outside of the current source file, they should be declared in a header file. Other source files can use these functions by including the header file or by declaring it (the former is recommended).

A good programming habit is to declare a prototype of a function in a header file. This makes it easy for the compiler to check for errors. When defining a function, the default function type is an external function. Such as:

void fun2 (void); extern void fun2 (void);

The function type is equivalent, but the previous one is the definition function, and the next one is declaring the function.

Summary

When writing programs, especially large programs, it is recommended to use the methods described above to make the necessary declarations and definitions of different variables and functions. Doing these details can be a great convenience for your programming, debugging, porting, and so on.





Dark Horse Programmer--c Language Foundation--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.