Search code examples
ccompilationstorage-class-specifier

Why can't the static and register storage classes be used together?


When defining a variable in the following manner:

static register int a1 = 0;

we get the error:

error: multiple storage classes in declaration specifiers

Is there any fundamental reason for this error? Why can't a variable be both stored in a register, and also be initialized only at start up/first call? It is possible to attach the register storage class to a global variable. <- edit: not true


Solution

  • If a compiler implemented what you wanted faithfully then it would tie up a CPU register for the length of your program. That's hardly practical.

    Remember that register is only advisory.