C Language Basic Knowledge Point parsing (extern,static,typedef,const) _c language

Source: Internet
Author: User
Tags modifier variable scope

One, the use of extern method

The following is an explanation of extern in the C language program:

In all source files of a source program, an external variable can only be defined once in a file, and other files are accessed through an extern declaration (the source file that defines an external variable can also contain an extern declaration for that external variable). The length of an array must be specified in the definition of an external variable, but the extern declaration does not necessarily specify the length of the array.

The initialization of an external variable can only appear in its definition.

Assuming that the function push and pop are defined in a file, and the variable Val and SP define the one in the other file that is initialized (which is usually not likely to be organized in this way), you need to bind these functions to the declaration by using the following definitions and declarations:

In file File1:

extern int sp;

extern double val[];

void push (double f) {...}

Double pop (void) {...}

In file File2:

int sp=0;

Double val[max_size];

Because extern declarations in file1 are placed not only outside of functions but also at the front of them, they apply to all functions in the file. For file1, such a set of statements would suffice. If you want to use and then define variables SP and Val in the same file, you also need to organize the files in this way.

Using popular language to summarize the use of extern, the shell is divided into the following three kinds of situations:

1 The declaration of the extern modifier variable. For example, if a file a.c needs to refer to the variable int v in B.C, you can declare an extern int V in A.C, and then you can reference the variable v. It should be noted here that the linked attribute of the referenced variable v must be an outer link (external), which means that a.c to refer to V, not just depending on the declaration of extern int V in A.C, but also on the variable v itself being able to be referenced. This involves another topic in C language-the scope of variables. A variable that can be referenced by another module as an extern modifier is usually a global variable. It is also important that extern int V can be placed anywhere in the A.C, such as you can declare an extern int v at the beginning of the function fun definition in A.C, and then you can refer to the variable V, except that you can only reference V in the function fun scope. This is also a problem with variable scope. For this, a lot of people are worried when they use it. It seems that an extern declaration can only be used for file scopes.

2 extern modifier function declaration. in essence, there is no difference between a variable and a function. A function name is a pointer to the beginning of a binary block of functions. If the file a.c need to refer to a function in B.C, such as in B.C, the prototype is int fun (int mu), you can declare the extern int a.c (int mu) in fun, and then you can do anything with fun. As with the declaration of a variable, an extern int fun (int mu) can be placed anywhere in the A.C, not necessarily in the scope of A.C file scopes. The most common method for referencing functions in other modules is the header file that contains the function declarations. What's the difference between using an extern and a include header file to refer to a function? extern is a much simpler way of referencing than a header file! The use of extern is straightforward, which function is declared with extern when you want to refer to it. This is probably a manifestation of the kiss principle! One obvious benefit of this is that it accelerates the process of compiling the program (or, rather, preprocessing) and saves time. In the large C program compilation process, this difference is very obvious.

3 In addition, the extern modifier can be used to indicate the invocation specification of a C or C + + function. For example, calling C library functions in C + + requires the use of extern "C" in the C + + program to declare the function to be referenced. This is for the linker, tell the linker to link in the C function specification. The main reason is that C + + and C program compilation is completed in the target code in different naming rules. The instructions for this usage are in the next article.

Here's a summary of the use of extern:

In C, the modifier extern is used before the declaration of a variable or function to indicate that "this variable/function is defined elsewhere, to be referenced here".

1. extern modifier variable declaration. For example, if a file a.c needs to refer to the variable int v in B.C, you can declare an extern int V in A.C, and then you can reference the variable v. It should be noted here that the linked attribute of the referenced variable v must be an outer link (external), which means that a.c to refer to V, not just depending on the declaration of extern int V in A.C, but also on the variable v itself being able to be referenced. This involves another topic in C language-the scope of variables. A variable that can be referenced by another module as an extern modifier is usually a global variable. It is also important that extern int V can be placed anywhere in the A.C, such as you can declare an extern int v at the beginning of the function fun definition in A.C, and then you can refer to the variable V, except that you can only reference V in the function fun scope. This is also a problem with variable scope. For this, a lot of people are worried when they use it. It seems that an extern declaration can only be used for file scopes.

2. extern modifier function declaration. in essence, there is no difference between a variable and a function. A function name is a pointer to the beginning of a binary block of functions. If the file a.c need to refer to a function in B.C, such as in B.C, the prototype is int fun (int mu), you can declare the extern int a.c (int mu) in fun, and then you can do anything with fun. As with the declaration of a variable, an extern int fun (int mu) can be placed anywhere in the A.C, not necessarily in the scope of A.C file scopes. The most common method for referencing functions in other modules is the header file that contains the function declarations. What's the difference between using an extern and a include header file to refer to a function? extern is a much simpler way of referencing than a header file! The use of extern is straightforward, which function is declared with extern when you want to refer to it. This is probably a manifestation of the kiss principle! One obvious benefit of this is that it accelerates the process of compiling the program (or, rather, preprocessing) and saves time. In the large C program compilation process, this difference is very obvious.

3. In addition, the extern modifier can be used to indicate the invocation specification of a C or C + + function. For example, calling C library functions in C + + requires the use of extern "C" in the C + + program to declare the function to be referenced. This is for the linker, tell the linker to link in the C function specification. The main reason is that C + + and C program compilation is completed in the target code in different naming rules.

Two, the use of static method

The following is an explanation of static in the C language program:

An external static declaration is often used for a variable, and of course it can be used to declare a function. Typically, a function name is globally accessible and is visible to parts of the entire program. However, if the function is declared as a static type, the function name is not visible except for the file that the function declaration is in.

Static can also be used to declare internal variables. The internal variables of the static type are the same as automatic variables, is a local variable of a particular function that can only be used in that function, but unlike an automatic variable, it always exists, regardless of whether the function it is in or not is called, rather than as an automatic variable that exists and disappears with the invocation and exit of the function. In other words, an internal variable of the static type is a variable that can only be used in a particular function but occupies space all the time.

Use popular parlance to explain three uses of static:

1 in the function body, a variable declared as static maintains its value unchanged during the invocation of this function. This variable also becomes a static local variable.

2 within the module (but outside the function), a variable declared as static can be accessed by functions used within the module, but not by other functions outside the module. It is a local static global variable.

3 within the module, a function declared as static can only be invoked by other functions within the module. That is, the function is limited to the local scope of the module in which it is declared.

Three, the use of the TypeDef method

The following is an explanation of the typedef in the C language program:

In any sense, the TypeDef declaration does not create a new type, it simply adds a new name to an existing type. The TypeDef declaration also does not add any new semantics, and the variables declared in this way have exactly the same attributes as those declared through normal declarations. In fact, a typedef is similar to a #define statement, but because the typedef is interpreted by the compiler, its text substitution function exceeds the processor's ability. For example:

typedef int (*PFI) (char *, char *);

The statement defines the type PFI as "a pointer to a function" that has two char* parameters, and the return value type is int *.

In addition to the simplicity of expression, there are two other important reasons for using TypeDef. First, it can parameterize the program to improve the portability of the program. If the data type declared by the TypeDef is related to the machine. Then, when the program is ported to another machine, you just need to change the TypeDef type definition. A frequently used case, for various sizes of reshaping, uses the type name defined by typedef, and then selects a suitable set of short, int, and long types for each of the different host hosts. There are some examples in the standard library, such as size_t and ptrdiff_t.

Four, the use of the Const method

The C-language const represents "immutable" and is as immutable as a constant, but the application scenario is different.

1) applied in variable

const char a= ' a ';

A= ' B '; Error, the value of variable a cannot be modified.

The variable a value cannot be changed at this time, and any statement that attempts to modify the value of a variable (for example, a=20;) will have an error.

2) applied to the pointer

Apply to * Left

const char *p;

char Const *P;

The above two statements act as if the value of the variable that the pointer points to cannot be modified, but the pointer can be modified.

For example 1;
const char *p= ' A ';

Char *q;

*p= ' B '; Wrong, the value that the pointer points to cannot be modified.

p=q; Yes, the pointer value can be modified

Apply to * right

Char *const *p;

Indicates that the pointer value (the position to which the pointer is pointing) cannot be changed, but the value that the pointer points to can be changed.

For example 2:

Copy Code code as follows:

Char *const *p= ' A ';

Char *q;

*p= ' B '; Yes, the value that the pointer points to can be modified.

p=q; Error, pointer value cannot be modified.


In short, the const is on the left, indicating that the value that the pointer points to cannot be modified. The const is on the right side, indicating that the pointer value (that is, where the pointer is pointing to) cannot be modified.

3) applied in function parameters

For example 3:strcat (char *a,char const *b), the string pointed to by parameter B is added to the end of the parameter a string.

At this point, the parameter *a value can be changed, but the *b value of the parameter cannot be changed

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.