Considering the example below:
header.h
extern int somevariable;
void somefunction();
source.cpp
#include "header.h"
somevariable = 0; // this declaration has no storage class or type specifier
void somefunction(){
somevariable = 0; // works fine
}
I couldn't find the answer to the following questions:
Why can't I just forward declare
int somevariable
and initialize it in the.cpp
file?Why is the
somefunction()
function able to "see"somevariable
when declared usingextern int somevariable
, and why is it not accessible outside of a function?