Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 305
Maor Veitsman Avatar asked Sep 14 '25 22:09

Maor Veitsman


2 Answers

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.

like image 144
R Sahu Avatar answered Sep 17 '25 13:09

R Sahu


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.

like image 45
Bathsheba Avatar answered Sep 17 '25 12:09

Bathsheba



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!