C language foundation knowledge parsing of extern static typedef const

  • 2020-04-02 01:53:01
  • OfStack

Use of extern

Here's how extern is explained in C programming:

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

Initialization of an external variable can only occur in its definition.

Assuming that the functions push and pop are defined in one file, and the variables val and sp are defined in another file that is initialized (which is not usually possible to organize the program in this way), these functions need to be "bound" to the declaration by 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 the extern declarations in file1 are placed not only outside of functions, but in front of them, they apply to all functions in this file. For file1, such a set of declarations is sufficient. If you want to use and define the variables sp and val in the same file, you should also organize the files in this way.

Summarizing the use of extern in plain English, the shell is divided into three cases:

1) declaration of extern modified variables. For example, if file a.c needs to refer to the variable int v in b.c, you can declare extern int v in a.c and then refer to the variable v. It is important to note here that the link attribute of the referenced variable v must be external, which means that a.c references v, not only by declaring extern int v in a.c, but also by the variable v itself being able to be referenced. This brings us to another topic in c -- the scope of variables. Variables that can be referenced by other modules with extern modifiers are usually global variables. It's also important to note that extern int v can be anywhere in a.c. For example, you can declare extern int v at the beginning of the function fun definition in a.c, and then you can refer to the variable v, but only in the fun scope. This is something that many people use with concern. It seems that extern declarations can only be used for file scopes.

2) extern modifier declaration. Essentially, there is no difference between a variable and a function. A function name is a pointer to the beginning of a function binary block. If file a.c needs to refer to a function in b.c, for example, where the prototype is int fun(int mu), then you can declare extern int fun(int mu) in a.c, and then you can use fun to do anything. Like the declaration of variables, extern int fun (int mu) can be anywhere in a.c, not necessarily within the scope of the a.c file scope. The most common way to refer to functions in other modules is the header file that contains the declaration of these functions. What's the difference between using extern and including header files to refer to functions? Extern references are much more concise than including header files! The use of extern is straightforward; you can declare any function you want to refer to with extern. This is probably an expression of the KISS principle. An obvious benefit of doing this is that it speeds up the process of compiling (or rather preprocessing) the program, saving time. This difference is evident during the compilation of large C programs.

3) in addition, the extern modifier can be used to indicate the calling specification for C or C++ functions. For example, if you call a C library function in C++, you need extern "C" in your C++ program to declare the function you want to reference. This is for linkers, telling them to use the C function specification when linking. The main reason is that C++ and C programs compile and have different naming rules in the object code. This usage is explained in the next article.

Here's an overview of extern:

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

1. Declaration of extern modified variables. For example, if file a.c needs to refer to the variable int v in b.c, you can declare extern int v in a.c and then refer to the variable v. It is important to note here that the link attribute of the referenced variable v must be external, which means that a.c references v, not only by declaring extern int v in a.c, but also by the variable v itself being able to be referenced. This brings us to another topic in c -- the scope of variables. Variables that can be referenced by other modules with extern modifiers are usually global variables. It's also important to note that extern int v can be anywhere in a.c. For example, you can declare extern int v at the beginning of the function fun definition in a.c, and then you can refer to the variable v, but only in the fun scope. This is something that many people use with concern. It seems that extern declarations can only be used for file scopes.

2. Extern function declaration. Essentially, there is no difference between a variable and a function. A function name is a pointer to the beginning of a function binary block. If file a.c needs to refer to a function in b.c, for example, where the prototype is int fun(int mu), then you can declare extern int fun(int mu) in a.c, and then you can use fun to do anything. Like the declaration of variables, extern int fun (int mu) can be anywhere in a.c, not necessarily within the scope of the a.c file scope. The most common way to refer to functions in other modules is the header file that contains the declaration of these functions. What's the difference between using extern and including header files to refer to functions? Extern references are much more concise than including header files! The use of extern is straightforward; you can declare any function you want to refer to with extern. This is probably an expression of the KISS principle. An obvious benefit of doing this is that it speeds up the process of compiling (or rather preprocessing) the program, saving time. This difference is evident during the compilation of large C programs.

3. In addition, the extern modifier can be used to indicate the calling specification for C or C++ functions. For example, if you call a C library function in C++, you need extern "C" in your C++ program to declare the function you want to reference. This is for linkers, telling them to use the C function specification when linking. The main reason is that C++ and C programs compile and have different naming rules in the object code.

Two, static use method

The following is the explanation of static in C programming language:

External static declarations are often used for variables, but they can also be used to declare functions. Typically, function names are globally accessible and visible to all parts of the program. However, if a function is declared as static, the function name is not visible to any file except the one in which the function is declared.

Static can also be used to declare internal variables. An internal variable of the static type, like an automatic variable, is a local variable of a particular function and can only be used in that function. However, unlike an automatic variable, it will always exist regardless of whether the function it is in is called or not. In other words, an internal variable of the static type is a variable that can only be used in a particular function but takes up space all the time.

Use colloquial terms to explain the three USES of static:

1) in the function body, a variable declared as static keeps its value unchanged during the function being called. This variable also becomes a static local variable.

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

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

Use method of typedef

The following is the explanation of typedef in C language programming:

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 this way have exactly the same properties as those declared in a normal declaration. In fact, a typedef is similar to a #define statement, but since a typedef is interpreted by a compiler, its text-substitution capability is more than the processor can handle. Such as:

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

This statement defines the type PFI as "a pointer to a function" that takes two char* arguments and returns an int *.

In addition to being more concise, there are two other important reasons for using typedefs. First, it parameterizes the program to improve its portability. If the data type declared by a typedef is machine-related. Then, when the program is ported to another machine, you only need to change the typedef type definition. A commonly used case is to use a type name defined with a typedef for a variety of shapers of different sizes, and then select an appropriate set of type short, int, and long sizes for each of the different hosts. There are some examples in the standard library, such as size_t, ptrdiff_t, and so on.

Iv. Usage of const

In C language, const stands for "immutable", which is basically the same as a constant, but the application scenario is different.

1) apply in variables

Const char a = 'a';

A = 'B'.   // error, the value of variable a cannot be modified.

At this point, the value of variable a cannot be changed, and any statement that attempts to modify the value of variable a (for example, a=20;) Both report errors.

2) apply to Pointers

Apply to the left of *

Const char * p;

Char const * p;

The above two statements have the same effect, which means that the variable value pointed to by the pointer cannot be modified, but the pointer can be modified.

For example, 1;
Const char * p = 'A';

Char * q;

* p = 'B';   // wrong, the value of the pointer cannot be modified.

P = q;   // yes, pointer values can be modified

Apply to the right of *

Const char * * p;

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

For example 2:


char *const *p='A';
char *q;
*P='b';  //Yes, the value of the pointer can be modified.
p=q;  //Error, pointer value cannot be modified.

In general, const is to the left of *, indicating that the value pointed to by the pointer cannot be modified. Const is to the right of *, indicating that the pointer value (i.e. the position to which the pointer points) cannot be modified.

3) applied to function parameters

For example, 3: strcat(char *a,char const *b), add the string to which parameter b points to the end of parameter a string.

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


Related articles: