Storage class in c programming

07 Sep 2022 Balmiki Mandal 0 C Programming

Exploring Storage Classes: A Guide for C Programmers

Storage classes in C programming are used to define the scope, lifetime, and visibility of a variable. There are four storage classes in C:

  • Automatic
  • External
  • Static
  • Register

Automatic variables are declared inside a function block or a compound statement. They are allocated memory when the function is entered and deallocated when the function exits. Automatic variables are visible only within the function block or compound statement in which they are declared.

External variables are declared outside of all functions and blocks. They are allocated memory when the program starts and deallocated when the program ends. External variables are visible to all functions in the program.

Static variables are similar to external variables, but they retain their values between function calls. Static variables are also visible to all functions in the program.

Register variables are stored in CPU registers rather than in memory. This can improve performance, but register variables can only be used for certain types of variables, such as integers and characters.

The following table summarizes the four storage classes in C:

Storage class Scope Lifetime Visibility
Automatic Function block or compound statement Allocated when the function is entered, deallocated when the function exits Visible only within the function block or compound statement in which they are declared
External Outside of all functions and blocks Allocated when the program starts, deallocated when the program ends Visible to all functions in the program
Static Outside of all functions and blocks Allocated when the program starts, deallocated when the program ends Visible to all functions in the program and retain their values between function calls
Register CPU registers Allocated when the variable is used, deallocated when the variable is no longer used Visible only within the function block or compound statement in which they are declared
 

Some examples of how to use storage classes in C:

 

C programming
// Auto storage class
int main() {
  int auto_variable = 10;
  printf("%d\n", auto_variable);
  return 0;
}

// Extern storage class
extern int extern_variable;

int main() {
  extern_variable = 20;
  printf("%d\n", extern_variable);
  return 0;
}

// Static storage class
static int static_variable = 30;

int main() {
  printf("%d\n", static_variable);
  return 0;
}

// Register storage class
register int register_variable = 40;

int main() {
  printf("%d\n", register_variable);
  return 0;
}
Output:
10
20
30
40

The storage class that you choose for a variable depends on your specific needs. If you need a variable to be visible only within a function block or compound statement, then you should use an automatic variable. If you need a variable to be visible to all functions in the program, then you should use an external or static variable. If you need a variable to be stored in a CPU register, then you should use a register variable.

 

Top Resources

What are advantages and disadvantages of external storage class?

Understanding Storage Memory, Default Value, Scope, and Life of Static and External Storage Class in C

What is the default storage class for local variables and global variables?

What is the default storage class for any function?

Enroll Now:

C-Programming From Scratch to Advanced 2023-2024] "Start Supercharging Your Productivity!"

Contact Us:

  • For any inquiries, please email us at [[email protected]].
  • Follow us on insta  [ electro4u_offical_ ] for updates and tips.

 

Note: If you encounter any issues or specific errors when running this program, please let me know and I'll be happy to help debug them!

BY: Balmiki Mandal

Related Blogs

Post Comments.

Login to Post a Comment

No comments yet, Be the first to comment.