Why does declaring an extern variable inside main() works,but not defining it inside main() as well?

后端 未结 4 1354
悲哀的现实
悲哀的现实 2021-01-14 04:29

This seems very trivial but a somewhat rigorous explanation for the following behavior will help my understanding of extern a lot.So I\'ll appreciate your answe

4条回答
  •  温柔的废话
    2021-01-14 05:05

    extern variable declaration is a promise to the compiler that there would be a definition of a global variable some place else. Local variables do not qualify as fulfillments of the promise to the compiler, because they are invisible to linkers. In a sense, extern declarations are similar to forward declarations of functions: you say to the compiler "I know this function is there, so let me use it now, and let linker take care of locating the actual implementation".

提交回复
热议问题