• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: linking error?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: linking error?


  • Subject: Re: linking error?
  • From: Andy Lee <email@hidden>
  • Date: Sun, 03 Aug 2008 18:44:35 -0400

On Aug 3, 2008, at 6:04 PM, Jeffrey Oleander wrote:
On Sat, 2008/08/02, Stephane Sudre <email@hidden> wrote:
You started programming in C 2 minutes ago?

I've been programming in C for about 22 years. That was about the same time I gave up trying to memorize fine points of every programming language's standard. To me, a "static" has nothing to do with what file it was declared in but in what program block it was declared, and that's why the "extern" is necessary to refer to it from elsewhere. So, what are we missing?

While I think the "2 minutes" remark may have been uncalled for (maybe it was meant as a joke?), and while I too have a limited memory for fine points (probably more so than you), you are definitely missing something.


The *scope* of any static variable is the block in which it's declared. If it's not in a block (i.e., not between any curly braces), the scope is limited to the file. It isn't visible outside of the file or block, and extern can't make it so. One consequence of this is that you can define static variables with the same name in different files.

The *lifetime* of a static variable in all cases is the duration of the program, i.e., the variable keeps its value even if you exit the block it was declared in.

A non-static variable that is not defined inside any block also lives for the duration of the program, but it *can* be referenced outside the file. You just have to declare the existence of the variable, using extern, before using it. You can *declare* it extern as many places as you want, but you can only *define* it (i.e., tell the compiler to allocate memory for it) in one place.

The key distinctions are between scope and lifetime, and between declaring and defining.

--Andy

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: linking error?
      • From: Michael <email@hidden>
References: 
 >Re: linking error? (From: Jeffrey Oleander <email@hidden>)

  • Prev by Date: Re: Optimal environment to compile C
  • Next by Date: Re: linking error
  • Previous by thread: Re: linking error?
  • Next by thread: Re: linking error?
  • Index(es):
    • Date
    • Thread