Vous êtes sur la page 1sur 22

Storage Classes

C has five storage classes


auto extern global variable static register

20070110 chap13

auto: Simple variables in a function block. Scope is limited to the function/block in which the variable is declared. Visible ONLY inside a function or block and exists only while the function/block is active.

Auto and Extern Variables


auto: default storage class of function parameters and local variables
Storage is automatically allocated on the stack at the time of a function call and deallocated when the function returns.

extern: storage class of names known to linker.


the name of the functions themselves

20070110 chap13

Global Variables
A variable that may be accessed by many functions in a program.
/* eg1.c */ int global_var_x; void afun(int n) /* eg2.c */ extern int global_var_x; void bfun(int n)

Only the defining declaration, the one in eg1.c, allocates spaces for global_var_x. A declaration beginning with the keyword extern allocated no memory; it simply provides information for the computer.
20070110 chap13 4

Output:

Output:

20070110 chap13

Static Variables
static: storage class of variables allocated only once, prior to program execution.
int fun_frag(int n) { static int once = 0; int many = 0;

many is allocated space on the stack each time fun_frag is called. Every time fun_frag returns, many is deallocated. Static variable once is allocated and initialized one time, prior to program execution. It remains allocated until the entire program terminates.
20070110 chap13 8

Register variables
Storage class of automatic variable that the programmer would like to have stored in registers. It is closely related to storage class auto and may be applied only to local variables and parameters. By choosing storage class register, the programmer indicates an expectation that the program would run faster if a register, a special high-speed memory location inside the central processor, could be used for the variable.

20070110 chap13

Storage Classes (contd.)


E.g:
void main () { int x; } // visibility of x is only inside this block void print () { printf ("something"); x = 10; // WRONG as x is not visible }

Storage Classes (contd.)


static:
Local variables:
Visible ONLY in function where declared Private to containing function (similar to auto). Retains values between function calls.

E.g:
int foo () { static int x; x++; return x; }

Notes on static (contd.)


E.g:
Using the foo function that we just wrote:
# include <stdio.h> int main () { printf ("%d\n", foo ()); printf ("%d\n", foo ()); }

Notes on static (contd.)


Running the program: [axgopala@nitrogen tmp]$ ./a.out 1 2 NOTE: value of x is now 2 !!!

Notes on static (contd.)


static:
Global variables:
Visible ONLY to functions inside the program unit where declared. Private to declaring module. Potentially visible to ALL functions within the program unit.

Notes on static (contd.)


E.g:
# include <stdio.h> int x = 0; // declaring a global variable x before main int main () { x = 2; add (); printf ("%d\n", x); } void add () { x++; } // x accessible out here also

Notes on static (contd.)


Running the program: [axgopala@nitrogen tmp]$ ./a.out 3 NOTE: value of x is 3, as it was increased by 1 inside the function add ().

Storage Classes (contd.)


extern:
Defined in some other program unit/module. Declared as locally accessible. Made available to calling modules through linking. Variables declared with extern cannot be initialized locally in functions.

Notes on extern
E.g: file ext.c
# include <stdio.h> int main () { extern int y; printf ("%d\n", y); }

Notes on extern (contd.)


E.g: file y.c # include <stdio.h> int y = 5; Running the program: [axgopala@nitrogen tmp]$ ./a.out 5 NOTE: value of y = 5 !!!

Notes on extern (contd.)


Cannot do this in main: # include <stdio.h>
int main () { extern int y; y = 10; // cant do this as y is an extern printf ("%d\n", y); }

Storage Classes (contd.)


register:
Creates/stores variables in CPU registers. Implementation dependent, sometimes does not work Only automatic variables and function arguments are permitted, not functions. If an unacceptable type or more variables than available registers, they are just ignored.

Notes on register
register:
Use of address operator (&) is ignored (as it is meaningless). Register variables can produce faster code since their values are not retrieved from memory (RAM). Frequently, optimizing compilers will generate register variables automatically.

Vous aimerez peut-être aussi