I am confused about this: what type of data can we store in register variables? I guess logically we can store any kind of data as register variables because they are variables, but really I am confused.

I have tried to search on internet and in my book. I couldn't find the answer. By book I am referring to ANSI C's programming book.

  • No its not for class I am studying on my own.I bought books and i watch tutorials on internet
    – Yousaf
    Jan 3, 2014 at 18:44
  • the answer is only yes or no in logicall terms. if we can store only integers or we can store any datatype as register variable
    – Yousaf
    Jan 3, 2014 at 18:46
  • yes they are binary so i guess any datatype in c is orginally binary and they can be stored as register. Got it
    – Yousaf
    Jan 3, 2014 at 18:50
  • and i saw that people only use integer type as register usually or they recommend to do so why is this ?. becuase othe datatypes take more space or other datatypes slows down the processing ? or both ?
    – Yousaf
    Jan 3, 2014 at 18:52
  • 1
    Are you talking about the register storage class in C, or physical registers in microchips?
    – user7043
    Jan 3, 2014 at 19:01

3 Answers 3


Let's look at what the ISO C standard actually says (I'll refer to the 2011 edition, a draft of which is available as N1570.


A declaration of an identifier for an object with storage-class specifier register suggests that access to the object be as fast as possible. The extent to which such suggestions are effective is implementation-defined.

Note that it says nothing about storing the object in a CPU register.

Elsewhere, the standard says that an external declaration may not use the register keyword; it can only be used for variables declared inside the body of a function.

Attempting to take the address of a register-qualified object is not allowed. (That means that declaring an array with register, though it's legal, is not particularly useful; you have to take its address to be able to do indexing).

Apart from that, a compiler is free to ignore the register keyword, treating it as equivalent to auto. You can apply the register keyword to any variable you like (as long as it's defined locally), regardless of its type or size -- but the only thing the compiler is obligated to do is to complain if you try to take its address.

A compiler is also free to store anything it likes in CPU registers, whether you use the register keyword or not, as long as the behavior is the same as if the variable were stored in memory. This includes storing a variable in a register for its entire lifetime (if the code happens not to try to compute its address), or retrieving its value from a register where the compiler knows it happens to be stored rather than re-loading from memory.

The common wisdom these days is that compilers are better than humans at deciding which variables should be placed in registers for greater speed, so the register keyword is of limited usefulness. In the Old Days, when systems were so small and slow that compilers couldn't afford to do that kind of analysis and dinosaurs roamed the Earth, register could be very useful. With modern compilers, not so much.

  • I think the conclusion from your answer is legally we can define any datatype as register but than its upto compiler to ignore it as register or not i think your answer is fairly clear and I think its best one. thansk @Keith Thompson for help.
    – Yousaf
    Jan 4, 2014 at 0:46
  • 2
    @Yousaf: Yes, that's basically it -- except that the compiler still has to complain about any attempt to take the variable's address, even if it doesn't actually store it in a CPU register. Jan 4, 2014 at 0:49
  • ok real thanks for the help and also all the other guys were also right but may be i have smaller mind and i couldnt grasp their hi-fi pro talk.
    – Yousaf
    Jan 4, 2014 at 0:51

The answer is neither yes nor no. The answer is it depends.

The register keyword never required the compiler to store the variable in a register. It is just a hint to the optimizer to preferentially allocate a register for that variable.

With register colouring implementations of modern compilers it's extremely unlikely that such hint will improve the situation, but it can easily make it worse. Or the optimizer will just ignore it, because it actually does know better. So in modern code, the recommendation is forget the keyword ever existed.

If the compiler actually honours the hint, it still depends on whether there is a register that can hold the type and whether it is available. All platforms have registers that can hold pointers and registers that can hold int. They may or may not have registers that can hold long, long long, double or long double. The compiler also may store derived types in one or more registers if they fit. When appropriate register exists, it may still be better used by other values, possibly implicitly generated by precalculation, subexpression elimination and similar optimizations. The compiler can actually calculate the cost of instructions it is about to generate and make a well informed decision about the register allocation.

And more importantly there is absolutely no way for you to tell when writing the code where the hint might possibly be helpful. The only way to know is to profile the code. So don't bother with register keyword before you've debugged your code, optimized all algorithms and eliminated any unnecessary work. Don't forget, premature optimization is the root of all evil and register is only optimization of last resort.

  • Slightly off topic, but which modern compiler actually uses graph coloring for register allocation? Both GCC and LLVM use heuristics. Good heuristics, and I agree with everything that follows, I'd just like to know.
    – user7043
    Jan 3, 2014 at 19:15
  • @delnan: The fact that they use heuristic algorithm does not change the nature of the problem itself and the heuristic is still based on the mathematical apparatus around graph colouring. Since the problem is NP-complete, I don't think any compiler searches exact solution; it would be too expensive.
    – Jan Hudec
    Jan 3, 2014 at 19:23
  • Okay, but under that definition "register coloring implementation" is not a measure of quality, as any heuristic - no matter how shitty - falls under it. Unless you introduce some subjective measure of how much it looks like a graph coloring algorithm if you squint enough?
    – user7043
    Jan 3, 2014 at 19:27
  • 3
    The actual Knuth quote: "Programmers waste enormous amounts of time thinking about, or worrying about, the speed of noncritical parts of their programs, and these attempts at efficiency actually have a strong negative impact when debugging and maintenance are considered. We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil. Yet we should not pass up our opportunities in that critical 3%." The abbreviated Knuth quote is the source of widespread misunderstanding about what he really meant. Jan 3, 2014 at 19:50
  • 1
    @Yousaf: That it depends on the platform, compiler, compiler options and exact piece of code. It's best left up to the compiler to decide.
    – Jan Hudec
    Jan 4, 2014 at 15:22

The register keyword in C can be used on any type of (local) variable and is only a hint to the compiler that you will be using that variable often, so the compiler should make accessing it as fast as possible. As keeping a variable in a processor register is often the fastest way to access it again, that was the name that got chosen for this hint.

Modern C compilers (less than about 20 years old) are good enough at determining which variables to keep in registers that they don't need hints like the register keyword, and it sometimes even causes a slowdown (when the compiler honors the hint and produces sub-optimal code as a result).

What types of values you can store in the physical registers of a processor depends on the processor architecture, but is typically limited to integer-like values (including pointers) and floating point values.

  • 1
    No, you can store any bits at all in registers, because types don't really exist at machine level. There just aren't many meaningful operations (aside from memory loads and stores) on bits that don't make meaningful integers or floats.
    – user7043
    Jan 3, 2014 at 19:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.