C ++ # include. h extern correlation and description,

Source: Internet
Author: User

C ++ # include. h extern correlation and description,

Both functions and variables are written in either voice or definition. The purpose of the Declaration is to tell other code the name of the variable, and the definition has a function more than the Declaration. In addition to the name, the variable will be allocated with memory.
For a function, adding or not adding extern does not matter, because a function body must be defined, and the addition of extern will also be ignored. If there is no function body, it is the declaration,

Int add (int a, int B );

Equivalent to extern int add (int a, int B );

If it is a variable
Int a; // defines a variable.
Extern int a; // declares a variable.
Variables can have any declaration, but only one definition is allowed. global variables are defined in multiple files. Only one file does not contain extern, And the rest must contain extern to indicate declaration.

Note: In the. h file, it is generally used to declare some variables. Not definitions. All definitions and implementations are in the. c or. cpp file.

Therefore, pay special attention

Note the following when defining a global variable, which is a global variable:

The declaration of global variables is generally written in. in the H file, add extern, because the definition variable is not added, so it is not correct. in this case, # include this. all files in the H file can access this global variable. however, this variable must be defined in. c or. cpp file. there can only be one definition of this variable.

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.