3

Why does the snippet of code below produce an error?

int fun(auto int arg)
{
    return 1;
}

If I use "register" in place of "auto", it works fine. Is there any reason behind this or is it just the way C standard is defined?

Valy
  • 573
  • 2
  • 12
Radha Gogia
  • 765
  • 1
  • 11
  • 22
  • 2
    [6.7.6.3p2](http://port70.net/~nsz/c/c11/n1570.html#6.7.6.3p2): The only storage-class specifier that shall occur in a parameter declaration is register. – Ilja Everilä Jul 21 '17 at 12:36
  • Perhaps this link can be useful: https://stackoverflow.com/questions/2192547/where-is-the-c-auto-keyword-used – Tony Tannous Jul 21 '17 at 12:36
  • 2
    Forget that "auto" and "register" exist. They haven't been relevant for the past 20-30 years. – Art Jul 21 '17 at 13:48

1 Answers1

3

Allowing register is likely an historical artifact from the times when compilers were not sophisticated enough to optimize register allocations as well or better than humans. Back then it was desirable to tell the compiler that a particular parameter should be passed in one of CPU's registers, not through a stack frame.

No other storage class specifier could possibly make sense here: you cannot pass parameters as extern or static, while auto would create confusion.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523