C Language: Storage classes 2

Register storage classes.

The keyword “register” is used to declare a variable of the register storage class.

Example:

register int a;

A register storage class variable is stored in CPU registers.

Properties

Storage CPU register.
Default initial value Garbage value.
Scope Local to the block in which the variables is defined.
Life Till the control remains within the block in which the variable is defined.

Example:1

#include<stdio.h>
int main()
{
	register int i;
	for(i=1;i<=100;i++)
	   printf("\n%d",i);
    return(0);
}

Note: A variable stored in a CPU register can always be accessed faster than the one which is stored in memory.

Therefore if we want to access a variable again and again then it is better to declare the variable of the register storage class.

We cannot use the register storage class for all types of variables. For example, the following declarations are wrong:

register float q;
register double a;
register long c;

This is because the CPU registers in a microcomputer are usually 16-bit registers and therefore cannot hold a float value or a double value, which requires 4 and 8 bytes respectively for storing a value.

However, if you use the above declarations you won’t get any error messages.

In such cases, the compiler would treat the variables to be of automatic storage class.

C Language Programming Tutorial

C Language Tutorial Home     Introduction to C Language     Tokens     If Condition      goto statement and Labelname     Switch Statements     For loop     While Loop     Do while loop     break and continue     Functions     Recursion     Inbuild Functions     Storage Classes     Preprocessor     Arrays     Pointers     Structures and Unions     File Handling     Projects