by Dinesh Thakur Category: Type And Variables

An object is a space in memory with a name or an identifier. The lifetime of an object is the storage duration of the object in a program, that is, lifetime is the portion of program execution during which the object exists with a constant address in the program and retains the last stored value. Three types of storage durations are defined in C, that is, automatic, static, and allocated.

An object whose identifier is declared without the storage-class specifier static and without any external or internal linkage has automatic storage duration, which ends (provided object is not a variable length array) with the end of execution of the block in which it is declared. It may or may not be declared with the specifier auto. For most of local variables, we do not use auto with them. An object whose identifier is declared with storage-class specifier static or with an internal or external linkage has static storage duration. Its lifetime is the entire execution of program and its stored value is initialized only once, prior to program startup.

Qualifiers Auto, Register, Extern, and Static

In C language, variables are not only the data type but also the storage class that provides information about their location and visibility.

Auto: It is a local variable known only to the function or block in which it is declared.

Register: A CPU has a number of registers for various purposes. A programmer may use the qualifier register for the declaration of a variable.

register int n = 90;

The variable value may be loaded on a register so that it is readily (in less time) available to the processor. The time of execution of program may decrease by this specification. However, it is left to the compiler to decide whether the variable can be loaded on a register.

Extern: The specifier extern gives the variable an external linkage.

Static: The static specifier gives internal linkage to variable.

Program illustrates extern, static, auto, and register qualifiers:

#include <stdio.h>

extern int n = 10;
int y= 5;
void main()
{
   static int D = 5;
   register int x =7, m;
   auto int K ;
   clrscr();
   K = y*y;
   m = x*x;
   printf("n * n = %d \t y * y = %d\n", n*n, K);
   printf ("m = %d\n", m*D );
}
The expected output is as given below.

C storage class Specifiers



About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.



Search Content