GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  Programming  >  C++
Go To First  |  Previous Question  |  Next Question 
 C++  |  Question 67 of 203    Print  
what are auto static variables and auto extern variables?

  
Total Answers and Comments: 5 Last Update: June 29, 2006     Asked by: Alok Singh Chauhan 
  
 Sponsored Links

 
 Best Rated Answer
Submitted by: mohit12379
 

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;
}
******************************************************************************



Above answer was rated as good by the following members:
wael.salman, yzesong
February 20, 2006 05:46:38   #1  
Azam        

RE: what are auto static variables and auto extern var...

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'


 
Is this answer useful? Yes | No
March 10, 2006 05:12:55   #2  
Deepu Abraham K        

RE: what are auto static variables and auto extern var...

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.


 
Is this answer useful? Yes | No
March 24, 2006 14:23:04   #3  
yogendra rajavat        

RE: what are auto static variables and auto extern var...
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
 
Is this answer useful? Yes | No
April 20, 2006 08:48:22   #4  
mohit12379 Member Since: March 2006   Contribution: 17    

RE: what are auto static variables and auto extern var...

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;
}
******************************************************************************


 
Is this answer useful? Yes | NoAnswer is useful 2   Answer is not useful 0Overall Rating: +2    
June 29, 2006 10:25:42   #5  
Biju Nair        

RE: what are auto static variables and auto extern var...
A variable declared in one file can be used in another file by declaring it as extern in the second file.
 
Is this answer useful? Yes | No


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape