#### Archived

This topic is now archived and is closed to further replies.

# What mean of extern?

## Recommended Posts

I found that some c/c++ programs using extern keyword. What is this? And when/where will we use extern? Also, is extern "c" == extern? What different? Thx

##### Share on other sites
you normally use extern in header files.

example: // this would be in game.h         extern int health; // can''t initialize only declare         // this allows you to use in any code or source file          // within that program think of it as a "super-global"         /*in game.c or game.cpp*/         int health = 100; // here is where you initialize it         // basically you can initialize it anywhere outside the         // header file it was declared in

extern "C" allows you to use C-specific functions/code in C++ files ... sorry don''t have an example

--Alpha

##### Share on other sites
simply...

extern int a;
means there is a variable called a declared somewhere in my source files... that variable should be global and shouldn''t be a static variable...

-----------------------------

DaHeR.RailgunMaster

##### Share on other sites
quote:
Original post by Anonymous Poster

extern "C" allows you to use C-specific functions/code in C++ files ... sorry don''t have an example

an example for that
  extern "C" void C_Code();// and the most commonextern "C" {void C_Code0();void C_Code1();}

-----------------------------

DaHeR.RailgunMaster

lol... thanks!

--Alpha

##### Share on other sites
quote:
Original post by wah_on_2
I found that some c/c++ programs using extern keyword. What is this?

It''s a directive telling the compiler that the definition of the function or variable will be found outside of the current translation unit and, therefore, will be fixed-up by the linker.
quote:

And when/where will we use extern?

Anytime its undesirable to define the entity within the current translation unit, such as when you link to library functions.
quote:

Also, is extern "c" == extern? What different?

They''re similar, but extern "C" tells the compiler to apply the local C linkage convention to entities that the extern applies to. Without the "C", the compiler will default to C++ linkage conventions. This protocol enables linking of declarations compiled using a C++ compiler with binaries compiled with a C compiler. Furthermore, the C and C++ standards allow vendors to extend the convention to link to binaries written using other languages, and you might see things such as extern "FORTRAN" or extern "PASCAL" in some environments.

• ### Forum Statistics

• Total Topics
628367
• Total Posts
2982284

• 10
• 9
• 13
• 24
• 11