Analysis of the extern statement in the C Language

Source: Internet
Author: User

1. Basic explanation

Extern can be placed before a variable or function to indicate that the definition of a variable or function is in another file, prompting the compiler to find its definition in other modules when it encounters this variable or function.

In addition, extern can be used to specify links.

2 problem: extern variable

An array is defined in a source file:

 
CharA [6];

In another file, the statement is declared using the following statement:

 
Extern Char*;

Can this be done?

Answer and analysis:

1) No,ProgramThe Operation will tell you illegal access. The reason is that,Pointer to type T is not equivalent to array of type T.Extern char * A declares a pointer variable instead of a character array. Therefore, it is different from the actual definition, resulting in invalid access during runtime. Change the Declaration to extern char a [].

2) The example analysis is as follows. If a [] = \ "ABCD \", then the external variable a = 0x61626364 (ASCII code value of ABCD), * A is obviously meaningless, for example:

500) This. style. width = 500; \ "onmousewheel = \" Return bbimg (this) \ ">
Obviously, the space (0x61626364) that a points to is meaningless and prone to illegal memory access.

3) This reminds us thatWhen using extern, you must strictly match the declared format.In actual programming, such errors are common.

4) extern is often used in variable declaration *. the C file declares a global variable. If the global variable is to be referenced, it is placed in *. H and use extern to declare.


3 problem: extern function 1

It is often seen that extern is placed before the function to become part of the function declaration,C LanguageWhat is the role of the keyword extern in the function declaration?

Answer and analysis:

IfThe function declaration contains the keyword extern, which only implies that the function may be defined in other source files and has no other function.. There is no obvious difference between the following two function declarations:

Extern int F (); and int f ();
Of course, such use is still available, that is, replacing include "*. h "to declare the function. In some complicated projects, I prefer to add the extern modifier before all the function declarations.

4 problem: extern function 2

When the function provider unilaterally modifies the function prototype, if the user does not know to continue to use the original extern statement, the compiler will not report an error during compilation. However, during the running process, a system error is often caused by missing or missing input parameters. How can this problem be solved?

Answer and analysis:

Currently, the industry does not have a perfect solution to deal with this situation. The common practice is that the provider provides external interface declarations in its own xxx_pub.h, and then the caller includes the header file, in this way, the extern step is omitted. To avoid such errors.

Baojian has a dual front. For the application of extern, different practices should be selected for different occasions.

5. Problem: extern "C"

When using C functions in the C ++ environment, the compiler often fails to find the C function definition in the OBJ module, resulting in a link failure. How can this problem be solved?

Answer and analysis:

C ++ LanguageTo solve the function polymorphism problem, the function name and parameter are combined to generate an intermediate function name.The C language does not. Therefore, the corresponding function cannot be found during the link.The C function needs to be linked with extern "C", which tells the compiler that keep my name and do not generate an intermediate function name for the link.

The following is a standard syntax:

  //   in. hfile header   
# ifdef _ cplusplus
# If _ cplusplus
extern \ " C \ "{
# Endif
# endif /* _ cplusplus */
...
...
// . h. End of the file
# ifdef _ cplusplus
# If _ cplusplus
}< br> # endif
#endif /* _ cplusplus */

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.