Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8426923
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T04:39:38+00:00 2026-06-10T04:39:38+00:00

@interface Foo : NSObject { extern int gGlobalVar; int i; } -(void)setgGlobalVar:(int)val; @end @implementation

  • 0
@interface Foo : NSObject

{
    extern int gGlobalVar;   
    int i;
}
-(void)setgGlobalVar:(int)val;

@end


@implementation Foo

-(void)setgGlobalVar:(int)val
{  
    i = 5;
    NSLog(@"i = %i", i);
    gGlobalVar = val;
}

@end

I can declare i in interface and use it in implementation without any errors. But I cannot declare a variable of the type extern in interface. Why is this so? Why do I get an error which says that: “Type name does not allow storage class to be specified”?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T04:39:40+00:00Added an answer on June 10, 2026 at 4:39 am

    Short Description:
    The bracketed section of a class’s @interface OR @implementation is only for declaring instance variables (aka “ivar”). The extern keyword is only for use with global variable declarations (or functions, but that’s another topic.)
    Therefore, you cannot declare an extern ivar.

    Gritty Details:
    Variables are first declared, and then defined. This distinction is typically blurred for variables in local scopes, as a locally declared variable without an explicit definition will often be allocated and given a default value by the compiler.

    Global variables are potentially available in any scope, provided that scope knows the global exists. That’s where the keyword extern comes in — it declares that the global variable exists, and was defined elsewhere. This is only useful when you want to access a global variable in different code files.

    Best Practices: Your book has some code that declares an extern variable in an implementation file (e.g. “.m” files, etc.)… that can work, but it’s a bad practice because you’re making potentially bad assumptions about whether that global actually has a valid definition elsewhere. (But, fancy compilers will discover this type of error.)

    Instead, the best practice is to declare an extern variable once in a header file, have an accompanying implementation file that’s dedicated to defining the externs in that header, and then include that header in other implementation files that want to use that global variable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

@interface foo: NSObject @property (nonatomic, retain) NSMutableArray *aMutableArray; @end @implementation @synthesize aMutableArray -(void)somefunction {
@interface A : NSObject - (void) tmp; - (void) foo; @end @implementation A -
Can I not do this in objective c? @interface Foo : NSObject { int
I have created a class named Foo : @interface Foo:NSObject{ int myInt; } @property
@protocol protoA <NSObject> @end @interface objA : NSObject<protoA> { @private } @end @implementation objA
Is this possible? public interface Foo<TBar> where TBar : (can use the '+' and
Assume that I have this piece of code: @interface Foo : NSObject { Bar
I have interface Foo public interface Foo { public void test(); } Class FooChild
This code is invalid: interface Foo { public void foo(final String string); } public
For example public interface X{ public void foo(X i); } public class Y implements

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.