Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Correct use of extern

Correct use of extern

Scheduled Pinned Locked Moved C / C++ / MFC
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    masnu
    wrote on last edited by
    #1

    I have read several sources on when to use the extern keyword and when not to, but I still have some confusion. Could someone explain the proper use of extern when it comes to variables and function: extern void MyFunction(void); extern int MyVariable; Thanks.

    D C 2 Replies Last reply
    0
    • M masnu

      I have read several sources on when to use the extern keyword and when not to, but I still have some confusion. Could someone explain the proper use of extern when it comes to variables and function: extern void MyFunction(void); extern int MyVariable; Thanks.

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      masnu wrote:

      Could someone explain the proper use of extern when it comes to variables and function: extern void MyFunction(void); extern int MyVariable;

      In both cases, it specifies external linkage (i.e., is visible from files other than the one in which it's defined).


      "The largest fire starts but with the smallest spark." - David Crow

      1 Reply Last reply
      0
      • M masnu

        I have read several sources on when to use the extern keyword and when not to, but I still have some confusion. Could someone explain the proper use of extern when it comes to variables and function: extern void MyFunction(void); extern int MyVariable; Thanks.

        C Offline
        C Offline
        Chris Losinger
        wrote on last edited by
        #3

        you use extern when you want to tell the compiler the type of a variable or function which is defined in another file. if you have a variable called blah, of type int, at global scope (not static, or local to a function) in foo.c and you want to use it in bar.c, you would put "extern int blah;" somewhere in bar.c, or in a file that bar.c includes. that way, when the compiler is working on bar.c, it knows the type of the variable 'blah'. the linker will come around later and make sure all references to 'blah' are looking at the same thing. Cleek | Image Toolkits | Thumbnail maker

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups