Sunday, 8 October 2017

C++ - confused about the extern keyword

So I am trying to understand it and what it is used for, I
get the difference between a declared and defined
variable.



I'll try to explain what I am confused
at using the examples in the tutorial.



Example
1:



extern int var;
int
main(void)
{
var = 10;
return
0;
}


so here
I get that you cannot assign var to 10 because no memory was allocated to
it.



Example
2:



#include
"somefile.h"
extern int var;
int
main(void)
{
var = 10;
return
0;
}


I'm
lost here. somefile.h would have something like int var = 0;to declare it correct? ut
then why would this program declare it again?

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print ...