What are auto static variables and auto extern variables?

Showing Answers 1 - 5 of 5 Answers

Azam

  • Feb 20th, 2006
 

in very simple way static mean auto static and same auto extern mean extern only. if you want to share any variable between object of same class then use static variable. Static variable mainly use for singalton class.

Extern use for exporting any finction form a dll in simple way  suppose you want to use any c code in other language then use extern.like extern 'c'

Deepu Abraham K

  • Mar 10th, 2006
 

In theory these are called storage classes. These will define the scope and life time of variables or functions.

There are mainly 4 of them:

auto is the default storage class for local variables.

register is used to define local variables that should be stored in a register instead of RAM. This means that the variable has a maximum size equal to the register size say a word.

static can also be defined within a function or as a global variable. If this is done, the variable is initalised at compilation time and retains its value between calls ie, inside the function. Because it is initialsed at compilation time, the initalistation value must be a constant. This is serious stuff - tread with care.

extern defines a variable as global one ie, that is visable to all files irrespective of where it is defined. When you use 'extern' the variable cannot be initalized as all it does is point the variable name at a storage location that has been previously defined.

  Was this answer useful?  Yes

yogendra rajavat

  • Mar 24th, 2006
 

basically these are storage classes 1)automatic2)register3)static4)externalthese storage classes tells us 4 thing1)initial value of variable if we r not assigning any value2)storage3)scope4)life

  Was this answer useful?  Yes

Neither there is a concept  of auto static var and Nor a auto extern varibales because auto var are local variable whose lifetime is within a block and located on stack area . Process will automatically assign stack memory when auto (local) variables come into picture and automatically get cleaned from stack memory when goes out of block of function.

NOTE :- extern, static, auto  varibles can't be mixed. Another thing is auto variable needs block means they should be declared and  use  in a block of function.

See Following Code and try at your home. Any goodthing you have pls write
me at mohit.gonduley@gmail.com

********************************************************************************
#include "stdio.h"
#include "stdlib.h"

auto int g_iVal = 90; // It  gives a compile time ERROR

int main(void)
{
 auto int iVal = 0; // It will WORK
 auto static int st_iVal = 10;// It gives  a compile time ERROR
 return 0;
}
******************************************************************************

Biju Nair

  • Jun 29th, 2006
 

A variable declared in one file can be used in another file by declaring it as extern in the second file.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions