Static variables have a property of preserving their value even after they are out of their scope! Hence, static variables preserve their previous value in their previous scope and are not initialized again in the new scope.
static data_type var_name = var_value;
Registers are faster than memory to access, so the variables which are most frequently used in a C program can be put in registers using register keyword. The keyword register hints to compiler that a given variable can be put in a register. It’s compiler’s choice to put it in a register or not. Generally, compilers themselves do optimizations and put the variables in the register.
register data_type var_name = var_value;
Differences between static variables and register variables in C.
|Static Variables||Register Variables|
|Keyword used is – “static”.||Keyword used is – “register”.|
|Static variable may be internal or external depending on the place of declaration.||Register variables are declared inside a function.|
|Internal static variables are similar to auto variables or local variables. Whereas, external static variables are similar to global variables.||Register variables are similar to auto or local or internal variables.|
|The execution speed is slower than register variables.||The register variables leads to faster execution of programs.|
|Internal static variables are active(visiblility) in the particular function and External static variables are active in the entire program.||Register variables are active only within the function.|
|Internal static variables are alive(lifetime) in until the end of the function and External ststic variables are alive in the entire program.||Register variables are alive until the end of a function.|
|Static variables stored in intialised data segments.||Register variables are stored in registers.|
|Static variable is stored in the memory of the data segment.||In register variables, CPU itself stores the data and access quickly.|
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Difference between Register Mode and Register Indirect Mode
- Difference between static and non-static variables in Java
- Difference between Static-1 and Static-0 hazard
- Difference between PC relative and Base register Addressing Modes
- Difference between Register and Memory
- Difference between Register and Buffer
- Difference between Cache Memory and Register
- Internal static variable vs. External static variable with Examples in C
- Initialization of global and static variables in C
- How are variables scoped in C - Static or Dynamic?
- What are the default values of static variables in C?
- Initialization of static variables in C
- Static Variables in C
- Understanding "register" keyword in C
- Register Allocations in Code Generation
- Difference between Static and Dynamic SQL
- Difference between Static and Shared libraries
- Difference between a Static Queue and a Singly Linked List
- Difference between Static and Dynamic Routing
- Difference between Static and Dynamic Web Pages
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.