use of extern
-
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
-
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
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_
-
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_
-
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
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 -
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