C language extern Learning 2 analysis

Source: Internet
Author: User

In the previous article, it was declared through a header file, and the call had a particularly large vulnerability:

Why the compiler can be chained, because the default is extern-decorated, such as the global scope of the function so that it can be called

Continue to strengthen learning:

This time there are two pairs of C files:

First.c first.h

SECOND.C second.h

FIRST.C Code:

// Multi-File testing " head.h "  <stdio.h>void  printstr () {    printf ("Hello world!\n  ");}

First.h Code:

void printstr ();   // function definition fields: extending from the declaration point to the end of the source program text

The corresponding, SECOND.C code:

" second.h "  <stdio.h>void  main () {printstr ();}

Second.h Code:(emphasis)

extern void printstr ();   // function definition fields: extending from the declaration point to the end of the source program text

Analysis:

Undoubtedly, in the above Second.h and first.h, we need to use the extern flag to modify the declaration of the PRINTSTR function, so that the PRINTSTR function can be exported to the connection program, which is implemented regardless of the first.c file in the call, Or in the SECOND.C file, the connector will be very smart to follow our wishes to connect him to the definition of the PRINTSTR function in the first.c file, without having to write the same printstr function in the second.c file.

However, the problem follows:

So how do we differentiate between which declarations in the header file are defined in their corresponding. c files, and which ones are not? This may not be necessary, because the Smart Connection program will not be necessary to help us find and export to the connector, regardless of which file is defined, but I think he does. Because we need to know what the specific content of this function is, what function it is, I may want to modify it after I have a new requirement, I need to find the definition of this function in a short time, then let me introduce a man-made specification in C language:

The function declared in the. h file, if defined in its corresponding. c file, does not use the extern modifier when declaring the function, and if it does not, it must be displayed using the extern modifier.

So, in the C-language. h file, we see two types of function declarations. With extern, not with extern, simple and straightforward, one is to refer to an external function, a function of its own life and definition.

Therefore, the extern modifier is not used in first.h (because the corresponding. c file has the definition of the child function Printstr)

Using the extern modifier in second.h

C language extern learning 2 analysis

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.