One of the questions that is still being asked in the interviews is about 'auto variables' and 'register variables'. I have never come across any practical code that uses them and dont even see any need for them being used but as I say, Interview questions dont reflect how good a coder would be and even if a person answers all questions correctly, how can we judge someone's debugging skills.
Anyway, 'auto' variables are the default local variables. When you define a local variable, it is by default 'auto'. There is no need to use it as its implicit. In the new C++ standard, C++0x, 'auto' serves another purpose and may break any C++ code using auto when compiled using C++0x.
The 'register' keyword was initially used to help the compiler optimise the performance for the variables that were read/written very often. Since the compilers have evolved, nowadays they choose to ignore the requests as they can optimize the performance better than us humans.
The following is from StackOverflow discussion:
Sample program as follows:
//Program tested on Microsoft Visual Studio 2008 - Zahid Ghadialy
using namespace std;
int i = 123;
auto int j = 456;
register int k = 789;
cout<<"Address of i = " << &i <<", Value = " << i << endl;
cout<<"Address of j = " << &j <<", Value = " << j << endl;
cout<<"Address of k = " << &k <<", Value = " << k << endl;
Output as follows:Note that the address of i, j and k are very close implying that the compiler chose to ignore the 'register' keyword.