What is difference between
int x=7;
and
register int x=7;
?
I am using C++.
Register keyword tells compiler to store the particular variable in CPU registers so that it could be accessible fast. From a programmer's point of view register keyword is used for the variables which are heavily used in a program, so that compiler can speedup the code.
Register is a keyword in C which suggests the system to use register as a memory for a variable instead of RAM. This accelerates the reading and writing of memory and enhances the overall performance. Note that using register does not gaurentee the use of system registers.
Register variables are similar to automatic variables and exists inside a particular function only. It is supposed to be faster than the local variables. If a program encounters a register variable, it stores the variable in processor's register rather than memory if available.
register is used to store the variable in CPU registers rather memory location for quick access. Static is used for both global and local variables. Each one has its use case within a C program. Extern is used for data sharing between C project files.
register
is a hint to the compiler, advising it to store that variable in a processor register instead of memory (for example, instead of the stack).
The compiler may or may not follow that hint.
According to Herb Sutter in "Keywords That Aren't (or, Comments by Another Name)":
A register specifier has the same semantics as an auto specifier...
According to Herb Sutter, register
is "exactly as meaningful as whitespace" and has no effect on the semantics of a C++ program.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With