Global variables in Objective-C - difference in extern and top of .m file declaration?

Extern is a way of explicitly stating, for readability and compile-time enforcement, that you are just declaring this variable here, and actually expect it to be defined elsewhere. If you were to also try to define the extern variable the compiler will tell you the error of your ways. This is useful for global variables to prevent name collision and multiple definitions, both of which will get you linker errors.

The extern keyword itself, however, does not make the variable global.

Up vote 2 down vote favorite 3 share g+ share fb share tw.

I know you can define a global variable in Objective-C by using "extern", but I just realized that the variables I had declared at the top of my . M file before my first method were also accidentally global (and that was causing some problems). I moved them into the @interface part of my header file, which I think correctly declares them as only existing within the class, which has solved some of my problems, but I am still a bit confused.

What is the difference in declaring a variable as extern and putting it at the top of a . M file? Or do those result in the same thing?

Objective-c variables global-variables global extern link|improve this question asked Dec 19 '10 at 4:14Jackson403217 72% accept rate.

Extern is a way of explicitly stating, for readability and compile-time enforcement, that you are just declaring this variable here, and actually expect it to be defined elsewhere. If you were to also try to define the extern variable the compiler will tell you the error of your ways. This is useful for global variables to prevent name collision and multiple definitions, both of which will get you linker errors.

The extern keyword itself, however, does not make the variable global. What does make the variable global is the position of its declaration in the file. If you were to declare a variable outside the @interface in a class' header file, you would have declared a variable that is shared across and visible to all instances of your class, as well as anyone who #imports the header.

If you were to (and apparently did) declare a variable outside of the @implementation in your class' . M file, you would have also have declared a variable that is shared between all instances of your class, but is not visible to anyone who #imports you header. So, use the position of variable declarations to determine scope.

You will only define these global variables in one place. For all other places that you declare them, prefix the declaration with extern to make the code readable, your intentions clear, and to make sure you don't also try and define it again.

Short version: extern means it exists somewhere, but not right here. Not having it inside @interface or @implementation makes it a global. – ughoavgfhw Dec 19 '10 at 6:50 Great explanation, thanks.

– diwup Dec 19 '10 at 6:59 Thanks for your help! – Jackson Dec 19 '10 at 7:30.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions