global variable的使用

/*
use of global variable, all functions can access to it.

numeric global variable is initialized to 0 automatically,
character global variable is initialized to NULL automatically

*/

#include
using namespace std;

void anotherFunction();
int num = 2;

int main()
{
    cout << "In main, num is " << num << endl;
    anotherFunction();
    cout << "Back in main, num is " << num << endl;
    return 0;
}

void anotherFunction()
{
    cout << "In anotherFunction, num is " << num << endl;
    num = 50;
    cout << "But, it is now changed to " << num << endl;//change the num to 50, and return it because it is global variable
}

你可能感兴趣的:(错难题)