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

前端 未结 3 463
慢半拍i
慢半拍i 2021-01-14 06:36

When defining a variable in the following manner:

static register int a1 = 0;

we get the error:

error: multiple storage cla         


        
相关标签:
3条回答
  • 2021-01-14 07:14

    The standard does not allow use of more than one storage-class specifier in a declaration.

    From the C99 Standard:

    6.7.1 Storage-class specifiers

    1 storage-class-specifier:

    typedef
    extern
    static
    auto
    register
    

    2 At most, one storage-class specifier may be given in the declaration specifiers in a declaration.

    0 讨论(0)
  • 2021-01-14 07:17

    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.

    0 讨论(0)
  • 2021-01-14 07:36

    The main reason is that the register qualifier implies that the variable has automatic storage duration. It is basically an auto variable for which you are telling the compiler that it would be good to place in a General Purpose CPU register.

    static qualifier implies a static or thread storage duration.

    Obviously the two are incompatible if applied to the same variable!

    It would be like asking to have a variable that die and survive (at the same time) when the activation record it belongs is removed.

    0 讨论(0)
提交回复
热议问题