|
| 1 | +--- |
| 2 | +Title: 'Storage Classes' |
| 3 | +Description: 'Define the lifetime, visibility, and scope of variables.' |
| 4 | +Subjects: |
| 5 | + - 'Code Foundations' |
| 6 | + - 'Computer Science' |
| 7 | +Tags: |
| 8 | + - 'Classes' |
| 9 | + - 'Storage' |
| 10 | + - 'Scope' |
| 11 | + - 'Objects' |
| 12 | +CatalogContent: |
| 13 | + - 'learn-c' |
| 14 | + - 'paths/computer-science' |
| 15 | +--- |
| 16 | + |
| 17 | +In C, **storage classes** determine where and for how long a variable is stored in memory, as well as its default value and scope. C provides four storage classes: |
| 18 | + |
| 19 | +1. `auto` |
| 20 | +2. `register` |
| 21 | +3. `static` |
| 22 | +4. `extern` |
| 23 | + |
| 24 | +Each storage class has a specific role in how variables behave in a program. |
| 25 | + |
| 26 | +## 1. `auto` Storage Class |
| 27 | + |
| 28 | +The `auto` storage class acts as the default class for local variables. Variables declared with `auto` are automatically created when the block of code (such as a function) is entered, and they are destroyed when the block is exited. These variables have automatic storage duration. |
| 29 | + |
| 30 | +Here is how a variable can be declared using the `auto` storage class: |
| 31 | + |
| 32 | +```pseudo |
| 33 | +auto type variable_name; |
| 34 | +``` |
| 35 | + |
| 36 | +Let's look at an example that demonstrates the behavior of the `auto` storage class: |
| 37 | + |
| 38 | +```c |
| 39 | +#include <stdio.h> |
| 40 | + |
| 41 | +int main() { |
| 42 | + auto int num = 5; // 'auto' is optional here as it's the default for local variables |
| 43 | + printf("%d\n", num); // Output: 5 |
| 44 | + return 0; |
| 45 | +} |
| 46 | +``` |
| 47 | + |
| 48 | +In this case, the `num` variable is created and destroyed within the `main` function. |
| 49 | + |
| 50 | +## 2. `register` Storage Class |
| 51 | + |
| 52 | +The `register` storage class suggests the compiler that a variable can be stored in a CPU register instead of RAM. This can improve the speed of access, but it is just a suggestion and not a guarantee. The `register` keyword can only be used for local variables. |
| 53 | + |
| 54 | +Here is how a variable can be declared using the `register` storage class: |
| 55 | + |
| 56 | +```pseudo |
| 57 | +register type variable_name; |
| 58 | +``` |
| 59 | + |
| 60 | +Below is an example demonstrating the `register` storage class: |
| 61 | + |
| 62 | +```c |
| 63 | +#include <stdio.h> |
| 64 | + |
| 65 | +int main() { |
| 66 | + register int count; |
| 67 | + for (count = 0; count < 5; count++) { |
| 68 | + printf("%d ", count); |
| 69 | + } |
| 70 | + return 0; |
| 71 | +} |
| 72 | +``` |
| 73 | + |
| 74 | +In this example, the `count` variable is suggested to be stored in a register for faster access during the loop. |
| 75 | + |
| 76 | +## 3. `static` Storage Class |
| 77 | + |
| 78 | +The `static` storage class is used to maintain the value of a variable across function calls. A static variable retains its value even after the function in which it is declared finishes executing. Additionally, static variables are initialized only once, and their lifetime is the entire program runtime. |
| 79 | + |
| 80 | +Here is how a variable can be declared using the `static` storage class: |
| 81 | + |
| 82 | +```pseudo |
| 83 | +static type variable_name; |
| 84 | +``` |
| 85 | + |
| 86 | +Let's see an example where a `static` variable retains its value across multiple function calls: |
| 87 | + |
| 88 | +```c |
| 89 | +#include <stdio.h> |
| 90 | + |
| 91 | +void counter() { |
| 92 | + static int count = 0; // This variable retains its value across function calls |
| 93 | + count++; |
| 94 | + printf("Count: %d\n", count); |
| 95 | +} |
| 96 | + |
| 97 | +int main() { |
| 98 | + counter(); // Output: Count: 1 |
| 99 | + counter(); // Output: Count: 2 |
| 100 | + counter(); // Output: Count: 3 |
| 101 | + return 0; |
| 102 | +} |
| 103 | +``` |
| 104 | + |
| 105 | +In this example, the `count` variable retains its value across multiple calls to the `counter` function, and its value continues from where it was left off in the previous call. |
| 106 | + |
| 107 | +## 4. `extern` Storage Class |
| 108 | + |
| 109 | +The `extern` storage class helps declare a variable that is defined in another file or elsewhere in the program. It tells the compiler that the variable exists, but its definition will be provided later. It is used for sharing variables between files. |
| 110 | + |
| 111 | +Here is how a variable can be declared using the `extern` storage class: |
| 112 | + |
| 113 | +```pseudo |
| 114 | +extern type variable_name; |
| 115 | +``` |
| 116 | + |
| 117 | +Below is an example demonstrating how the `extern` storage class is used across files: |
| 118 | + |
| 119 | +```c |
| 120 | +// file1.c |
| 121 | +#include <stdio.h> |
| 122 | + |
| 123 | +int count = 10; // Definition of count |
| 124 | + |
| 125 | +void display() { |
| 126 | + printf("Count: %d\n", count); |
| 127 | +} |
| 128 | + |
| 129 | +// file2.c |
| 130 | +extern int count; // Declaration of count from file1.c |
| 131 | + |
| 132 | +int main() { |
| 133 | + display(); // Output: Count: 10 |
| 134 | + return 0; |
| 135 | +} |
| 136 | +``` |
| 137 | + |
| 138 | +In this example, the `extern` keyword in `file2.c` allows access to the `count` variable defined in `file1.c`. |
| 139 | + |
| 140 | +Each of these storage classes provides different advantages, depending on how and where the variable is used in the program. |
| 141 | + |
| 142 | +The table below provides a concise comparison of the four storage classes in C, outlining their lifetime, scope, initialization behavior, and typical use case: |
| 143 | + |
| 144 | +| Storage Class | Lifetime | Scope | Initialization | Use Case | |
| 145 | +| ------------- | ---------------------- | ------------------------------ | ---------------------------- | -------------------------------------------- | |
| 146 | +| `auto` | Local function/block | Local function/block | Automatic | Default for local variables | |
| 147 | +| `register` | Local function/block | Local function/block | Automatic | Suggests variable to be stored in a register | |
| 148 | +| `static` | Entire program runtime | Local function/block or global | Zero (if not initialized) | Retains value between function calls | |
| 149 | +| `extern` | Entire program runtime | Global | External (defined elsewhere) | Access variables from other files | |
0 commit comments