Under what circumstances can an extern variable be used in definition?

后端 未结 6 1224
情书的邮戳
情书的邮戳 2020-12-21 12:16

I am very very sorry. I didn\'t know my incomplete code attachment would create such a mess. I am very glad to see so many sincere helps.

This code will compile:

6条回答
  •  悲&欢浪女
    2020-12-21 12:38

    extern int main_stat=-3;  
    

    declares and defines main_stat, While:

    extern int main_stat;      
    

    just declares the variable main_stat.

    You can have as many declarations as you want but you can have only one Definition.

    The keyword extern, indicates External Linkage. Without it main_stat would be static and have Internal linkage and you cannot not use main_stat from another translation unit.

    Is storage allocated in the stack frame of myadd ?

    No definitely not on the stackframe of add.
    Where the memory is allocated is implementation defined but you have the assurance that the object will be alive throughout the duration of the program.

提交回复
热议问题