tags:

views:

295

answers:

5

Possible Duplicate:
“register” keyword in C?

What is difference between

int x=7;

and

register int x=7;

?

I am using C++.

+11  A: 

register is a hint to the compiler, advising to store that variable in a register of the processor instead of memory (for example, instead of the stack) .

The compiler may or may not follow that hint.

Found this lying around here:

A register specifier has the same semantics as an auto specifier...

Tom
credits of the link to @Fred Larson
Tom
+7  A: 

With today's compilers, probably nothing. Is was orginally a hint to place a variable in a register for faster access, but most compilers today ignore that hint and decide for themselves.

KeithB
+1  A: 

Almost certainly nothing.

register is a hint to the compiler that you plan on using x a lot, and that you think it should be placed in a register.

However, compilers are now far better at determining what values should be placed in registers than the average (or even expert) programmer is, so compilers just ignore the keyword, and do what they wants.

James Curran
+4  A: 

According to Herb Sutter, it is "exactly as meaningful as whitespace."

Fred Larson
He's among us http://stackoverflow.com/users/297582/herb-sutter xD
Johannes Schaub - litb
@Johannes: I updated the links. Maybe that's more appropriate.
Fred Larson
+1  A: 

In C++, any program which is valid that uses the keywords "auto" or "register" will be semantically identical to one with those keywords removed (unless they appear in stringized macros or other similar contexts). In that sense the keywords are useless for properly-compiling programs. On the other hand, the keywords might be useful in certain macro contexts to ensure that improper usage of a macro will cause a compile-time error rather than producing bogus code.

supercat
An example of the last bit may be useful.
Dennis Zickefoose