Storage Classes - C Programming Professional Made easy (2015)

C Programming Professional Made easys (2015)

Chapter 5 Storage Classes

Storage class refers to the lifetime and visibility of functions and variables within a C program. Such specifiers are found before the type they modify. Some of the most commonly used storage classes in a C program include auto, static, register, and extern. So how are these storage classes used?

Auto

This storage class is considered as the default storage class for all the local variables. Consider this example:

{

int mount;

auto int month;

}

The above example has variables with a similar storage class. Take note that auto may only be used inside functions, such as local variables.

Register

This storage class is typically used for local variables that have to be stored inside a register instead of a RAM. The variable should have a size that is less than or equal to the size of the register. It should also not use the unary ‘&’ operator because it does not include a memory. Look at this example:

{

register int kilometers;

}

Keep in mind that the register storage class will only be functional if used with variables that use quick access like counters. Also, you should take note that this storage class does not necessarily store the variable inside a register. Instead, it offers the possibility of storing that variable inside a register based on certain implementations of restrictions and hardware.

Static

This storage class commands the compiler to save a local variable throughout the lifetime of the program rather than destroying and creating it whenever it comes in and out of range. Turning your local variables into static allows them to retain their values amid function calls.

In addition, you can use the static modifier for global variables. If you do this, the scope of the variable can be regulated in the file in which it has been declared. Remember that if you use static on a member of the class data, all its class objects can share just one copy of its member.

#include <stdio.h>

/* function declaration */

void func(void);

static int count = 5; /* global variable */

main()

{

while(count--)

{

func();

}

return 0;

}

/* function definition */

void func( void )

{

static int i = 5; /* local static variable */

i++;

printf("i is %d and count is %d\n", i, count);

}

The above sample program containing the storage class static yields the following result:

i is 6 and count is 4

i is 7 and count is 3

i is 8 and count is 2

i is 9 and count is 1

i is 10 and count is 0

Extern

This storage class is used to provide a global variable reference that can be seen in all program files. So whenever you use extern, you cannot initialize the variable because all it can do is point the name of the variable towards a specific storage location in which it has been defined before.

If you have several files but you want to define a global function or variable that you intend to use in your other files as well, you can use extern to provide a function or variable reference. You should keep in mind that extern is for declaring a global function or variable in another file. If there are several files that share similar global functions or variables, the extern modifier is used. Consider this example:

First File: main.c

#include <stdio.h>

int count ;

extern void write_extern();

main()

{

write_extern();

}

Second File: write.c

#include <stdio.h>

extern int count;

void write_extern(void)

{

count = 5;

printf("count is %d\n", count);

}

In this example, extern is defined in the first file and is used to declare count in the second file. If you compile both files as

$gcc main.c write.c

You can have the results of 5