Wednesday, 6 July 2016

What happens when extern “C” is used without block in the program?

No comments:
Guess the output?


extern "C" int myGlobalVar;

extern "C" int myGlobalVar2;
int myGlobalVar2;

extern "C" { int myGlobalVar3; }

int main()
std::cout<< myGlobalVar << " " << myGlobalVar2 << " " << myGlobalVar3 << std::endl;
return 0;


Linker error.


All statement in program, except the following lines is fine in the program.

std::cout<<  myGlobalVar << " " << myGlobalVar2 << " " << myGlobalVar3 << std::endl;

So, what is the problem in above lines?
myGlobalVar is not defined rather only it is only declared.
Following statement only declares the variable.

extern "C" int myGlobalVar;

To declare and define the variable following statement has to be used or declare the variable again like I did for myGlobalVar2.

extern "C" { int myGlobalVar3; }

No comments:

Post a Comment