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. Managed C++/CLI
  4. use of extern

use of extern

Scheduled Pinned Locked Moved Managed C++/CLI
helpquestion
5 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.
  • S Offline
    S Offline
    Shamnar
    wrote on last edited by
    #1

    hi all i am a beginer programmer. so u might have feel bore to read this problem. i dont know what is the use of extern. i wrote a file like int i=10; then save it as t.h then i included it in my program as #include "t.h" then i got the value of i just by using printf("%d",i); it printed the value of i 10 but i thouht that i would have to use the statement extern int i; to get the desired output? if we dont want extern here what is the exact use of extern. pls help me. shamnar

    _ 1 Reply Last reply
    0
    • S Shamnar

      hi all i am a beginer programmer. so u might have feel bore to read this problem. i dont know what is the use of extern. i wrote a file like int i=10; then save it as t.h then i included it in my program as #include "t.h" then i got the value of i just by using printf("%d",i); it printed the value of i 10 but i thouht that i would have to use the statement extern int i; to get the desired output? if we dont want extern here what is the exact use of extern. pls help me. shamnar

      _ Offline
      _ Offline
      _AnsHUMAN_
      wrote on last edited by
      #2

      In one .CPP file declare the variable at global space int ix; In the file that you want the variable to be used or say stdafx.h that is included in all the other files write: extern int ix; The extern keyword confirms to the compiler that "ix" is an int declared in some other .CPP file. Somethings seem HARD to do, until we know how to do them. ;-) _AnShUmAn_

      S 1 Reply Last reply
      0
      • _ _AnsHUMAN_

        In one .CPP file declare the variable at global space int ix; In the file that you want the variable to be used or say stdafx.h that is included in all the other files write: extern int ix; The extern keyword confirms to the compiler that "ix" is an int declared in some other .CPP file. Somethings seem HARD to do, until we know how to do them. ;-) _AnShUmAn_

        S Offline
        S Offline
        Shamnar
        wrote on last edited by
        #3

        but without using extern, just by an include statement i got the desired o/p. why do we go 4 such a complex concept?:( shamnar

        S 1 Reply Last reply
        0
        • S Shamnar

          but without using extern, just by an include statement i got the desired o/p. why do we go 4 such a complex concept?:( shamnar

          S Offline
          S Offline
          S Senthil Kumar
          wrote on last edited by
          #4

          Without extern, every translation unit (cpp file) in which you included i gets its own i. That is, each translation unit reserves memory for the variable. Did you try including thhe file in multiple cpp files? You'll find that the code won't link, you'll get linker errors complaining about multiple symbol redefinitions for i. The reason is the same, each cpp file has a global i, so the linker doesn't know which i to use. The solution is to define the variable in one cpp file and to declare the variable extern in the header file. This way, all cpp files including the header files will recognize that variable is defined somewhere else. Does this answer your question? Regards Senthil _____________________________ My Blog | My Articles | WinMacro

          S 1 Reply Last reply
          0
          • S S Senthil Kumar

            Without extern, every translation unit (cpp file) in which you included i gets its own i. That is, each translation unit reserves memory for the variable. Did you try including thhe file in multiple cpp files? You'll find that the code won't link, you'll get linker errors complaining about multiple symbol redefinitions for i. The reason is the same, each cpp file has a global i, so the linker doesn't know which i to use. The solution is to define the variable in one cpp file and to declare the variable extern in the header file. This way, all cpp files including the header files will recognize that variable is defined somewhere else. Does this answer your question? Regards Senthil _____________________________ My Blog | My Articles | WinMacro

            S Offline
            S Offline
            Shamnar
            wrote on last edited by
            #5

            thank you.......... shamnar

            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