For working professionals
For fresh graduates
More
Explore C Tutorials: From Begi…
1. Introduction to C Tutorial
2. Addition of Two Numbers in C
3. Anagram Program in C
4. Armstrong Number in C
5. Array in C
6. Array of Pointers in C
7. Array of Structure in C
8. C Program to Find ASCII Value of a Character
9. Assignment Operator in C
10. Binary Search in C
11. Binary to Decimal in C
12. Bitwise Operators in C
13. Boolean in C
14. C Compiler for Mac
15. C Compiler for Windows
16. C Function Call Stack
17. C Language Download
18. Operators in C
19. C/C++ Preprocessors
20. C Program for Bubble Sort
21. C Program for Factorial
22. C Program for Prime Numbers
23. C Program for String Palindrome
24. C Program to Reverse a Number
25. Reverse a String in C
26. C string declaration
27. String Input Output Functions in C
28. Calculator Program in C
29. Call by Value and Call by Reference in C
30. Ceil Function in C
31. Coding Vs. Programming
32. Command Line Arguments in C/C++
33. Comments in C
34. Compilation process in C
35. Conditional Statements in C
36. Conditional operator in the C
37. Constant Pointer in C
38. Constants in C
39. Dangling Pointer in C
40. Data Structures in C
41. Data Types in C
42. Debugging C Program
43. Convert Decimal to Binary in C
44. Define And include in C
45. Difference Between Arguments And Parameters
46. Difference Between Compiler and Interpreter
47. Difference Between If Else and Switch
48. Do While Loop In C
49. Double In C
50. Dynamic Array in C
51. Dynamic Memory Allocation in C
52. Enumeration (or enum) in C
53. Evaluation of Arithmetic Expression
54. Factorial of A Number in C
55. Features of C Language
56. Fibonacci Series Program in C Using Recursion
57. File Handling in C
58. For Loop in C
59. Format Specifiers in C
60. Functions in C
61. Function Pointer in C
62. goto statement in C
63. C Hello World Program
64. Header Files in C
65. Heap Sort in C Program
66. Hello World Program in C
67. History of C Language
68. How to compile a C program in Linux
69. How to Find a Leap Year Using C Programming
70. Identifiers in C
71. If Else Statement in C
72. If Statement in C
73. Implementation of Queue Using Linked List
74. Increment and decrement operators in c
75. Input and Output Functions in C
76. How To Install C Language In Mac
77. Jump Statements in C
78. Lcm of Two Numbers in C
79. Length of an Array in C
80. Library Function in C
81. Linked list in C
82. Logical Operators in C
83. Macros in C
84. Matrix multiplication in C
85. Nested if else statement in C
86. Nested Loop in C
87. One Dimensional Array in C
88. Operator Precedence and Associativity in C
89. Overflow And Underflow in C
90. Palindrome Program in C
91. Pattern Programs in C
92. Pointer to Pointer in C
93. Pointers in C: A Comprehensive Tutorial
94. Pre-increment And Post-increment
95. Prime Number Program in C
96. Program for Linear Search in C
97. Pseudo-Code In C
98. Random Access Files in C
99. Random Number Generator in C
100. Recursion in C
101. Relational Operators in C
102. Simple interest program in C
103. Square Root in C
104. Stack in C
105. Stack Using Linked List in C
106. Static function in C
107. Stdio.h in C
108. Storage Classes in C
Now Reading
109. strcat() in C
110. Strcmp in C
111. Strcpy in C
112. String Comparison in C
113. String Functions in C
114. String Length in C
115. String Pointer in C
116. strlen() in C
117. Structures in C
118. Structure of C Program
119. Switch Case in C
120. C Ternary Operator
121. Tokens in C
122. Toupper Function in C
123. Transpose of a Matrix in C
124. Two Dimensional Array in C
125. Type Casting in C
126. Types of Error in C
127. Unary Operator in C
128. Use of C Language
129. User Defined Functions in C
130. What is Variables in C
131. Is C language case sensitive
132. Fibonacci Series in C
Storage classes in C are a set of keywords that guide the compiler in storing and manipulating variables in memory. Each storage class has its own rules and behaviour, including scope, lifetime, and linkage.
Storage classes in C form the backbone of memory management, providing control over variable storage, scope, and duration. They help define the visibility and lifespan of a variable or function within a C program, enabling the programmer to manage data and memory more efficiently.
Storage classes in C are used to specify the lifetime, scope, and visibility of a variable or function. These attributes guide how the program handles memory storage, data visibility, and data retention across multiple function calls. The precise use of storage classes in C will become clearer as you keep reading and encountering the different types of storage classes in C and understand their attributes!
Wondering how many storage classes in C are there? There are four types of storage classes in C:
Each of these storage classes has distinct rules, limitations, and behaviours. Let’s dive deep into each of these classes in the subsequent sections!
The auto storage classes in C are the default storage class for all local variables. The lifetime of these variables is limited to the function they are declared in, and they get destroyed once the execution of the function block is complete.
#include<stdio.h> |
In the above code, the auto int x variable is automatically destroyed after each function call. So, it prints '1' each time func() is called instead of incrementing.
Compared to other storage classes, the key difference of an automatic storage class is its limited lifetime, which is only valid within the duration of its function block.
Static storage class in C, represented by the keyword 'static', has a lifetime that exists for the entire execution of the program. This means that the value of a static variable persists between function calls.
#include<stdio.h> |
In this code, the static int x variable retains its value between function calls. So, it prints '1' on the first call and '2' on the second.
Unlike automatic variables, static variables persist in their value between function calls. Their scope, however, remains limited to the block they're defined in.
The register storage class in C, denoted by the keyword 'register', advises the compiler to store the variable in the CPU's register instead of RAM for faster access.
#include<stdio.h> |
In this example, the register int x variable is suggested to be stored in the CPU register.
The register class is similar to the auto class, which suggests the compiler stores the variable in a CPU register.
External storage class in C, represented by the keyword 'extern', is used to give a reference of a global variable that is visible to all program files. When a variable is declared with the 'extern' keyword, it indicates that the variable is defined in another file and the current file is using its reference.
#include<stdio.h> |
In this example, the extern int x variable refers to the global int x variable.
The extern storage class provides a method to access global variables across different files, making it unique compared to the other storage classes.
The scope of a variable refers to the part of the program where the variable can be accessed. The lifetime of a variable refers to the period during which the variable exists in the memory during the execution of the program.
Each storage class in C has its own rules that determine the scope and lifetime of a variable. For instance, automatic variables have a scope limited to the function block they are declared in, and their lifetime ends when the execution of the function block is completed.
Using storage classes effectively requires understanding their properties and the best practices related to them. Here are some best practices to follow:
1. Local Variables: Use the 'auto' keyword for variables that are only required within a single function or block. This ensures that the variable's scope is limited, preventing potential conflicts with variables of the same name in other functions or blocks.
void function() { |
2. Persistent Variables: Use the 'static' keyword for variables that need to maintain their value between function calls but should not be accessible outside their function. This is useful for functions that need to keep track of their state between calls.
void function() { |
3. Performance-Critical Variables: Use the 'register' keyword for variables that require quick access, such as counters in intensive loops. But remember, using the 'register' keyword is merely a suggestion to the compiler, and it is up to the compiler to put the variable in a register or not.
void function() { |
4. Global Variables: Use the 'extern' keyword to refer to variables that are used across several files. Declare the variable in one file and use 'extern' in any other files where the variable is needed.
main.c:
nt globalVariable = 0; // Declare global variable |
utils.c:
extern int globalVariable; // Reference to the global variable |
1. Initialization of Extern Variables: Do not use 'extern' to declare a variable in the file where it is initialised. This might lead to complications. An 'extern' variable is a reference to a variable declared elsewhere, so it should not be initialised where it is declared.
Incorrect:
extern int globalVariable = 0; // Incorrect |
Correct:
extern int globalVariable; // Correct |
2. Scope Confusion: Do not use a variable with the same name in different scopes without understanding the implications. This can lead to confusion and bugs that are hard to trace. If a variable with the same name is declared in a smaller scope, it will overshadow the larger one.
int variable = 0; // Global scope |
3. Uninitialized Variables: Do not forget to initialise 'static' and 'extern' variables as they retain their value across different function calls. Uninitialized 'static' and 'extern' variables can lead to unpredictable program behaviour.
Incorrect:
static int uninitializedVariable; // Incorrect, contains garbage value |
Correct:
static int initializedVariable = 0; // Correct, initialized to 0 |
An excellent example of the use of storage classes in real-world programming can be seen in the design of operating systems. System calls in operating systems often use static variables to retain state information between different calls.
Consider the simple case of a system call that reads a file in chunks:
#include<stdio.h> |
In this example, the readFileInChunks function uses static variables to hold the file pointer and a buffer. These variables maintain their state between function calls, enabling the function to remember its position in the file and read it in chunks.
Register variables are commonly used in performance-critical applications such as video games, embedded systems, or high-frequency trading platforms where a delay of microseconds can have substantial impacts.
Here is an example of a game loop that might use a register variable:
#include<stdio.h> |
In this example, the frame counter in the game loop is declared as a register variable. This suggests the compiler to try and store this variable in a CPU register for faster access. While it's up to the compiler whether to take this suggestion or not, using the register keyword in this way may lead to performance improvements.
In large projects split over multiple source files, the 'extern' keyword can be used to indicate that a variable is declared in another file. This way, the variable can be shared across different files.
Consider a simple case where a program is split into two files, 'main.c' and 'utils.c'. The global variable 'globalVar' is declared in 'main.c' and used in 'utils.c'.
main.c:
#include<stdio.h> |
utils.c:
#include<stdio.h> |
In the above example, extern int globalVar in 'utils.c' references the int globalVar declared in 'main.c', thus sharing the variable across files.C
Understanding storage classes in C is essential to becoming a proficient C programmer. Each storage class has its own specific use cases and behaviour, such as defining variable scope, lifetime, and linkage.
Mastering the use of storage classes can significantly enhance your efficiency and effectiveness as an expert programmer. This allows you to better manage and control how your variables are stored, accessed, and manipulated.
Ready to dive deep into C programming? Check out the comprehensive Executive Post Graduate Programme in Software Development course provided by upGrad. Under the guidance of IIIT-B, this course strives to solidify your knowledge and elevate your programming skills.
1. Why do we use storage classes in C?
Storage classes in C are used to specify the scope, lifetime, and visibility of a variable or a function in a program.
2. What is the default storage class in C?
'auto' is the default storage class for local variables in C.
3. Can a register variable be a global variable?
No, a register variable cannot be global, as its scope is limited to the block in which it is declared.
Take a Free C Programming Quiz
Answer quick questions and assess your C programming knowledge
Author
Start Learning For Free
Explore Our Free Software Tutorials and Elevate your Career.
Talk to our experts. We are available 7 days a week, 9 AM to 12 AM (midnight)
Indian Nationals
1800 210 2020
Foreign Nationals
+918045604032
1.The above statistics depend on various factors and individual results may vary. Past performance is no guarantee of future results.
2.The student assumes full responsibility for all expenses associated with visas, travel, & related costs. upGrad does not provide any a.