top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What are the different storage classes in C?

+7 votes
705 views
What are the different storage classes in C?
posted Dec 3, 2013 by Prachi Agarwal

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

5 Answers

+1 vote
 
Best answer

There are four storage classes in C, which are following -

auto - Storage Class
auto is the default storage class for all local variables.

{
  int Count;
  auto int Month;

}
The example above defines two variables with the same storage class. auto can only be used within functions, i.e. local variables.

register - Storage Class

register is used to define local variables that should be stored in a register instead of RAM. This means that the variable has a maximum size equal to the register size (usually one word) and cant have the unary '&' operator applied to it (as it does not have a memory location).

{
   register int  Miles;
}

Register should only be used for variables that require quick access - such as counters. It should also be noted that defining 'register' goes not mean that the variable will be stored in a register. It means that it MIGHT be stored in a register - depending on hardware and implimentation restrictions.

Static - storage class
The keyword used for Static storage class is 'static'. The variable declared as static is stored in the memory. Default value of that variable is zero. Scope of that variable is local to the block in which the variable is defined. Life of variable persists between different function calls.

External - storage class
The keyword used for External storage class is 'extern'. The variable declared as extern is stored in the memory. Default value of that variable is zero. Scope of that variable is global. Variable is alive as long as the program’s execution doesn’t come to an end.

answer Dec 3, 2013 by Meenal Mishra
+1 vote

A storage class defines the scope (visibility) and life time of variables and/or functions within a C Program , Auto,Static,Register,Extern.
 Static: The static storage class instructs the compiler to keep a local variable in existence during the lifetime of the program instead of creating and destroying it each time it comes into and goes out of scope. Therefore, making local variables static allows them to maintain their values between function calls
voidfunc( void )
{
Static int i = 5; /* local static variable */
i++;
printf("i is %d and count is %d\n", i, count);
}

answer Dec 3, 2013 by Giri Prasad
+1 vote

auto, static, extern, register

answer Dec 3, 2013 by sivanraj
+1 vote

There are two storage classes in C :

  1. Static
  2. Automatic

Static variable has a value throughout the execution of the program. All functions have global lifetimes.

Automatic variables, or variables with local lifetimes, are allocated new storage each time execution control passes to the block in which they are defined. When execution returns, the variables no longer have meaningful values.

C provides the following storage-class specifiers:
1. auto
2. register
3. static
4. extern
5. typedef

Items declared with the auto or register specifier have local lifetimes. Items declared with the static or extern specifier have global lifetimes.
A typedef declaration creates an alias for a type

answer Dec 4, 2013 by Alok Kumar
0 votes

C has three types of storage: automatic, static and allocated. Variable having block scope and without static specifier have automatic storage duration.

Variables with block scope, and with static specifier have static scope. Global variables (i.e, file scope) with or without the the static specifier also have static scope. Memory obtained from calls to malloc(), alloc() or realloc() belongs to allocated storage class.

answer Dec 4, 2013 by Neeraj Pandey
...