I have seen an interview question from Bloomberg from here: http://www.careercup.com/question?id=383154
extern void print(int *a, int z);
void print(int *a, int z);
Question 1> Will it compile?
Question 2> If it compiles, what does it mean?
extern int i;
int i;
Question 3> Will it compile?
Question 4> If it compiles, what does it mean?
Answer 1
Yes, it compiles successfully.
Answer 2
A global function without a body
{}is automatically a function declaration. The keywordexternis optional. So both of them are just declarations. And multiple declarations are just fine.Answer 3
Yes, it compiles successfully.
Answer 4
One is a declaration and the other is definition. Note that this definition doesn’t have an initializer though. For global variables, you need explicitly add keyword
externto distinguish a declaration with a definition. And it’s perfectly conform to One Definition Rule in C++.Edit: I cannot open the link there. I assume the code you mentioned is in a global context.