1. C Programming for Beginners: Writing Your First Hello World Program in C in 2023 2. Mastering Datatypes in C Programming: A Comprehensive Guide for Beginners in 2023 3. Complete Guide to % Format Specifiers in C for Beginners in 2023 4. A Comprehensive Guide to Operators in C Programming: Learn Everything About Operators in C in 2023 5. Master C Operator Precedence and Associativity in 2023: Definition, Examples, and Code 6. Learn Increment and Decrement Operators in C: A Beginners Guide with Examples - Codzify 7. Mastering Bitwise Operators in C: A Comprehensive Guide with Examples - Codzify 8. Ternary Operators in C Programming: A Beginners Guide (2023) 9. Switch Statements in C 2023: Complete Guide for Beginners 10. Learn Functions in C Programming - A Complete Tutorial for Beginners in 2023 11. Call by Value and Call by Reference in C Programming - A Complete Tutorial for Beginners in 2023 12. Storage Classes in C Programming: A Comprehensive Guide for 2023 13. Arrays in C Programming: A Comprehensive Guide for 2023 14. Basics of 2D Array in C Programming in 2023: Definition, Initialization, and Accessing Elements 15. Beginners Guide to Strings in C Programming with Codes and Examples 16. Strings Functions in C Language - strlen, strcpy,strcmp,strcat functions 17. A Comprehensive Guide to Structures in C Programming in 2023 18. Learn Unions in C Programming with Codes, Examples, and Explanations in 2023 19. Dynamic Memory Allocation in C: Understanding malloc, calloc, realloc, and free Functions 20. Introduction to C Programming 21. Hello world program in C 22. String functions in C - strncat, strcpy, strncpy, strchr, strrchr, strstr functions 23. Program for Fibonacci number in C - Codzify.com 24. What is palindrome number in C? 25. Write a C program to find a factorial of a given number 26. Write a C program to check whether a given number is prime or not 27. Write a C program to check whether a given number is an Armstrong number or not. 28. Write a C Program to transpose a matrix 29. C Programming Tutorial: Understanding Comments and Identifiers with Code Examples 30. Master String Handling Functions in C in 2023 - Codzify Topics

Storage Classes in C Programming: A Comprehensive Guide for 2023

Article by: Manish Methani

Last Updated: November 3, 2021 at 2:04pm IST
5 mins 6 sec read

In this tutorial, we'll cover the basics of storage classes in C programming, including their types, scope, and usage. We'll also provide some code examples to help you better understand how they work.

What are Storage Classes in C Programming?

Storage classes in C programming are used to define the scope, visibility, and lifetime of variables and functions. There are four types of storage classes in C programming: automatic, static, register, and extern.

Automatic Storage Class

The automatic storage class is the default storage class for variables declared inside a function. These variables are created when the function is called and destroyed when the function exits. They are not visible to other functions and cannot be accessed outside the function where they are declared.

Here's an example of how to declare a variable using the automatic storage class:

void myFunction() {
   int x; // automatic storage class
}

Static Storage Class

The static storage class is used to declare variables that retain their values even after the function exits. These variables are initialized only once, and their value is preserved between function calls. They have global scope within the file where they are declared and can be accessed from any function within that file.

Here's an example of how to declare a variable using the static storage class:

void myFunction() {
   static int x; // static storage class
}

Register Storage Class

The register storage class is used to declare variables that should be stored in a processor register instead of memory. These variables are usually used for frequently accessed data to improve program efficiency. However, it is up to the compiler to decide whether or not to store the variable in a register.

Here's an example of how to declare a variable using the register storage class:

void myFunction() {
   register int x; // register storage class
}

What is the Extern Storage Class in C Programming?

The extern storage class is used to declare variables and functions that are defined in other files or modules. When a variable or function is declared with the extern keyword, it tells the compiler that the definition of the variable or function will be provided later in another file or module.

In other words, the extern storage class is used to establish the global scope of a variable or function, allowing it to be used in multiple files or modules.

How Does the Extern Storage Class Work?

When you declare a variable or function with the extern keyword, you are telling the compiler that the definition of the variable or function is not in the current file or module. Instead, the definition is in another file or module, and the compiler needs to look for it at link time.

Here's an example of how to declare a variable using the extern storage class:

// File 1
extern int x; // declare variable x in another file or module

void myFunction() {
   // use variable x
}
// File 2
int x; // define variable x in this file or module

int main() {
   // initialize variable x
   x = 10;
   
   // call function that uses variable x
   myFunction();
}

In this example, we have two files: File 1 and File 2. File 1 declares the variable x using the extern keyword, indicating that its definition is in another file or module. File 2 defines the variable x and initializes it to a value of 10. When File 1's function myFunction is called, it uses the variable x without having to know its definition because it was declared with the extern keyword.

In this tutorial, we covered the basics of storage classes in C programming. We explained the different types of storage classes and how they are used to define the scope, visibility, and lifetime of variables and functions. We also provided some code examples to help you better understand how they work. By understanding the different storage classes, you can write more efficient and organized code.

Explore Tech Guide:

Codzify Logo

Terms and Conditions    Cookie Policy   Refund Policy   Adsense Disclaimer

Contact: teamcodzify@gmail.com