answersLogoWhite

0

Answer

Global variables and extern variables are very similar, but with a major difference. Let's say you have a global variable in your header file, like this:
( Refer how u can add the variable declaration in the source code as extern, i think we cant add into header files as it said)
int x = 0;

Now, in each file that includes that header file, there will be a new variable called x. Modifying x in file1.cpp won't modify x in file2.cpp.

Now, for externs... Say you have something like this in your header:

extern int x;

Then, in file1.cpp (which includes your header):

int x = 0;

Then, in file2.cpp (also includes the header):

x = 46;

x in file1.cpp is also now equal to 46. You see, when you use an extern variable, you're telling the compiler that you want to use that variable across all of your source files. If you use a regular global, however, you create a new variable in each of your source files.

Anyway, that's just for variables, which I assume you're talking about - global and extern functions are a bit different, and I'm not entirely clear on them.

User Avatar

Wiki User

15y ago

What else can I help you with?

Continue Learning about Engineering

What is the difference between extern and global?

Extern and Global are the storage space in C program. Global provides us to access the variables from anywhere inside the program only whereas Extern provides us to access the variables from outside that program, i,e., some other program.


What is the difference between static function and global static function?

'global static'?! There is no such thing.


What is external variable in c language?

The "extern" declaration in C is to indicate the existence of, and the type of, a global variable or function. A global variable, or a global function, is one that is available to all C modules (a single C module is typically a single .c file). An extern is something that is defined externally to the current module. In many cases, you can leave off the extern qualifier and not notice any difference because the linker can collapse multiple definitions to one. But the intent is then unclear in the code, and the code is error prone in case of typos. It is much clearer to define the global in one place, and then declare extern references to it in all the other places. When refering to globals provided by a library, especially a shared library, this is even more important in order to ensure you are talking about the correct, common instance of the variable. Declaring a variable as extern will result in your program not reserving any memory for the variable in the scope that it was declared. For instance (as example) if a program's source code declared the variable var as a global volatile int in foo.c, to properly use it in bar.c you would declare it as extern volatile int var. It is also not uncommon to find function prototypes declared as extern. A good C manual will certainly answer this more completely.


Difference between int a and extern int a. why the first one is definition while the second is declaration please explain?

The declaration 'int a' both declares the variable of 'a' and allocates memory for it. When you use 'extern' you are referring to a variable called 'a' that has its memory allocated in another module. The actual variable 'a' is not in the same compilation unit as the current one being compiled. Where the variable 'a' is located is resolved by the linker. When using 'extern' you state your intent to use a variable called 'a', but it doesn't reserve any memory for it in the current module.


What value is assigned to extern variable?

Default initial value of extern integral type variable is zero otherwise null.

Related Questions

What is the difference between extern and global?

Extern and Global are the storage space in C program. Global provides us to access the variables from anywhere inside the program only whereas Extern provides us to access the variables from outside that program, i,e., some other program.


Difference between global and transnational strategy?

the difference between global and international strategy


Is global variables are declared within the main function?

Might be, but don't forget the keyword 'extern':int main (void){extern int errno;...}


Difference between international and global communication?

what is the difference between international communication and global communication


Difference between global optimization and partial optimization?

essential diffrence between global and local optimization


What is the difference between global depletion of ozone and the hole in the ozone layer?

There is no difference between the two. The global depletion of ozone is the hole over ozone only.


What is the difference between static function and global static function?

'global static'?! There is no such thing.


Is there a difference between international politics and world politics?

what is the difference between global politics and international politics


What is the difference between declaration and initialization?

Perhaps an example will help. extern int value; /* declaration */ int value; /* definition */ int value= 20; /* definition with initialization */


What is the difference between global and world?

None, same meaning.


What is the difference between global war and world war?

None


What is the difference between local and global revision?

that they are not the same thing