Home  >  Article  >  php教程  >  Detailed explanation of global variables and extern

Detailed explanation of global variables and extern

高洛峰
高洛峰Original
2016-12-19 14:42:171557browse

Declaration and Definition

First let’s talk about declaration and definition

Declaration is not equal to definition. The declaration only points out the name of the variable and does not allocate storage space for it; the definition points out the name of the variable and allocates storage space for the variable. The definition contains Declare

extern int i; //Declare variable i, but no storage space is allocated and cannot be used yet. It can occur many times. The following three situations can only occur once

int i; //Variable i is defined and After allocating space, you can use

extern int a =0 //Define a global variable a and give it an initial value

int a =0; //Define a global variable a and give it an initial value


Note: A variable can be declared multiple times in a program, but it can only be defined once.

Global variables: Variables defined inside a function are called local variables, and their scope is from the point of definition to the end of the function; variables defined outside the function are called global variables, and their scope is from the point of definition to the end of the file.

Whether it is a global variable or a local variable, the scope starts from the place of definition


extern

extern is used to declare global variables

#include<iostream>using namespace std;int main(){
    extern int a;
    cout<<a<<endl;
    //int a=5; this is wrong , a should be a global variable    getchar();
    return 0;
}int a=5;//global variable

Use #include to include variables and functions in other header files declaration, why do we need the extern keyword? If I want to reference a global variable or function a, I just need to include #includef309a191f3373e0d27442a5a200f12ba directly in the source file (xxx.h contains the declaration of a). Why should I use extern?


test.h

#include<iostream>
using namespace std;

int changea();
//int temp2=1; 如果在此定义temp2的情况下,main.cpp和test.cpp都包含了test.h,会造成temp2的重复定义 

extern int temp3;

test.cpp

#include "test.h"

int temp1=1000;
//int temp2=10; 如果不注释掉会出错 
int temp3=100;

extern const int temp4=400;

main.cpp

#include <cstdlib>#include <iostream>#include "test.h"using namespace std;int main(int argc, char *argv[]){
    extern int temp1;
    cout<<"temp1 is"<<temp1<<endl;
    
    extern int temp4;
    cout<<"temp4 is"<<temp4<<endl;
    
    //extern int temp2;    //cout<<"temp2 is"<<temp2<<endl;    
    cout<<"temp3 is"<<temp3<<endl;
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

output:

temp1 is1000

temp4 is400

temp3 is100


temp1: declared in test.cpp int temp1=1000, use temp1 in main.cpp, first declare that temp1 is an external variable

temp2: defined in test.h, both main.cpp and test.cpp include test.h, which will cause duplication of temp2 Definition, comment out the program to compile

temp3: declare in test.h, define in test.cpp, use in main.cpp

temp4: const defaults to a local variable, even if it is declared globally , and an initial value must be assigned when defining. If you want to reference it externally, you must add extern

In short, the principle is: if you want to use a global variable in other files, you must define extern type variablename, and then you can use it

If the const variable wants to be used by other files, before the definition Need to add extern


For more global variables and extern related articles, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn