Compare Automatic, Register, Static and External Variables in C
Let us understand the difference between various storage classes in C programming language. Below is the table that will show difference between Automatic, Register, Static and External Variables storage Classes in C.
Feature | Automatic Variable | Register Variable | Static Variable | External Variable | |
---|---|---|---|---|---|
1 | Keyword Used | auto | register | static | extern |
2 | Storage | Memory | CPU registers | Memory | Memory |
3 | Default initial value | Garbage Value | Garbage Value | Zero Value | Zero Value |
4 | Scope | Local to the block in which the variable is defined | Local to the block in which the variable is defined | Local to the block in which the variable is defined | Global |
5 | Life | Till the control remains within the block in which the variable is defined | Till the control remains within the block in which the variable is defined | Value of the variable persists between different function calls | As long as the program’s execution doesn’t come to an end |
6 | Use | General purpose use. Most widely used compared to other storage classes | Used extensively for loop counters | Used extensively for recursive functions | Used in case of variables which are being used by almost all the functions in a program |
Very neatly given !