Learning notes -- extern keywords

Source: Internet
Author: User

Function of the extern keyword

1. Declare external variables or functions

When the variables or functions you need are in another file, in addition to include, you can also use extern to declare external variables or functions.

 

// File1.cpp
Int;
Int fun (int w)
{
Return ++ w;
}

// Main. cpp
# Include <iostream>
Using namespace std;
Int main ()
{
Extern int a; // declare an external variable
Extern int fun (int w); // declare an external Function

A = 5;
Cout <a <endl; // 5
Cout <fun (a) <endl; // 6
Return 0;
}
Note that the extern keyword only serves to declare and does not allocate memory space. The declared format must be strictly consistent with the defined format. Www.2cto.com

 

2. the extern "C" link is specified as the C function library.

Parameter Overloading is a new feature of C ++, which is not available in C. When writing code using C ++, if an error occurs when calling the C function (for the cause of the error, see the implementation mechanism of C ++ overload ), you need to declare the extern "C" to specify the link as the C function library.


Extern "C"
{
/**/
}


From C Xiaojia

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.