plese anyone can tell me about storage classes and their use in progg. >>>>>>

Storage classes specify where an object is stored, its visibility (linkage) and how long it exists (storage duration).

There are three forms of linkage (summarizing):

  • external: The object is visible across the entire program.
  • internal: The object is visible only to a single file.
  • none: Basically a "super internal" linkage; the object is visible only to its scope where it doesn't explicitly have external or internal linkage.

There are also three types of storage duration:

  • automatic: The object exists until the end of its scope.
  • static: The object exists until the program terminates.
  • dynamic: The object exists until it is explicitly released.

Storage Classes

typedef: Technically it's not a storage class, but you use this when you want to define a synonym for an existing type:

typedef int foo; /* Now foo and int are interchangeable types */

extern: This specifies that an object has external linkage and static storage duration, meaning the object is visible across the entire program after compilation and linking and exists until the program terminates. The typical use of extern is "not defined here", meaning the object isn't defined in the current file, but a declaration is still required for successful compilation:

/* second.c */
extern int foo = 12345;
/* main.c */
/* Compiled with second.c */
#include <stdio.h>

int main ( void )
{
  extern int foo;

  printf ( "%d\n", foo );

  return 0;
}

You'll find extern declarations most often in headers, because it's a bad practice to define an object within a header. The actual definition is localized to a single source file, while any other file using the header can access it due to the declaration.

static: This is an overloaded storage class depending on the scope of the object. If the object is at file scope, static changes the linkage to internal (the default linkage is external) and still specifies static storage duration. If the object is in a block scope, static changes the storage duration to static.

Because the static storage class will change the linkage to internal at file scope, you can specify both variables and functions that are only visible within the file. In object-oriented parlance, those objects are private to a file:

/* extern by default, visible everywhere */
void foo();

/* Same as above */
extern void bar();

/* Visible only to this file! */
static void baz();

It's generally a good practice to limit the visibility of your objects, so static is a critical component for good design in C. Data hiding is one of the two common ways static is used in C.

The other common use for static is to maintain a persistent state between calls of a function while still limiting the scope of the object to the function:

#include <stdio.h>

void foo()
{
  static int x = 0;

  printf ( "%d\n", x++ );
}

int main ( void )
{
  int i;

  for ( i = 0; i < 10; i++ )
    foo();

  return 0;
}

auto
The auto storage class is useless because everywhere you can use the auto keyword, it's already the default. It applies automatic storage duration and no linkage. Local variables that are not extern or static are auto.

register
The register storage class is also useless because it's largely ignored by compilers. The idea behind it is as a hint to compilers that the object will be used a lot and to store the value in a machine register. It applies automatic storage duration and no linkage, but also imposes extra restrictions such as not being able to take the address of the object.

commented: A good reference page. +17
commented: Very nicely put. +23

Hye h!!

There are basically three types of storage classes. They are:
1)Atomic Storage
2)Register Storage
3)External
4)Static

These all are used depending on programmers choice and their need in the program.
Their respective variables are called
Atomic variable: e.g.- auto int a; //a is considered as atomic variable
Register variable: e.g- register int a //a is now register variable
External Variable: e.g.- Declaring variable outside the function is by default an external
Static variable: e.g.- static int a //now a is static variable

They are stored at the different location according to their storage type and are having defined scope(life time: access to that variable).
Atomic: Life time is within the block and stored probably in run time stack.
Register: Life time is until the end of block in which they are declared and stored in cpu register or probably in run time stack.
External: Life time is until the entire program is terminated
Static: Life time is within program and stored either in data or base segment.

>There are basically three types of storage classes.
You listed four.

>1)Atomic Storage
Atomic? Yeah, you need to read up on the definition of that word. :icon_rolleyes: The correct term is "automatic", which sounds similar but doesn't mean the same thing.

>Static: Life time is within program and stored either in data or base segment.
Base segment, huh? Somehow I get the feeling that you're taking one of my answers to a question and butchering it with misunderstanding. When I said bss segment, I really did mean "bss" (it's an acronym for "Block Started by Symbol"; it wasn't a typo.

>1)Atomic Storage
That must be the memory involved in nuking your program when the water cooling system fails in your computer.

[Edit:] I know it was an "innocent misspelling". Just making a funny comment.

>That must be the memory involved in nuking your program
>when the water cooling system fails in your computer.
No, that's nukular storage.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.