Correlation and description of C + + #include. h extern

Source: Internet
Author: User

Both functions and variables have a declaration and a definition of two notation. The purpose of a declaration is to tell other code the name of the variable, whereas the definition has a function that is more than a declaration, and allocates memory for that variable in addition to the name.
For a function, adding no extern has no relation, because the function body must be defined, and the extern will be ignored, and no function body is the declaration,

int add (int a, int b);

equivalent to extern int add (int a, int b);

If it's a variable.
int A; Defines a variable a
extern int A; Declares a variable a
A variable can have any declaration, but there can be only one definition, the global variable is defined in multiple files, only one file is non-extern, and the rest must be declared with an extern representation.

Description: In the. h file, it is generally used to declare some variables. Not defined, all definitions and implementations are in. C or. cpp files.

So, pay special attention to

When defining global variables, it is important to pay special attention when it is a global variable:

The declaration of a global variable is generally written in the. h file, plus extern, because it is not defined as a variable, so it is not. This global variable can be accessed by all files that include the. h file. However, the definition of this variable must be in a. C or. cpp file. And there can only be one definition of this variable.

Correlation and description of C + + #include. h extern

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.