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

前端 未结 3 464
慢半拍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条回答
  •  萌比男神i
    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.

提交回复
热议问题