Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Storage-Class Specifiers are used to determine two independent properties?

From Storage-class specifiers:

The storage-class specifiers determine two independent properties of the names they declare: storage duration and linkage.

So, for example, when static keyword is used on global variables and functions (who's storage class is static anyway) it sets their linkage to Internal-linkage. When used on variables inside functions (which have no linkage) - it sets their storage class to static.

My question is: why is the same specifier used for both things?

like image 479
Gil-Mor Avatar asked Apr 19 '17 14:04

Gil-Mor


People also ask

What is the purpose of storage class specifiers What are they?

Storage Classes are used to describe the features of a variable/function. These features basically include the scope, visibility and life-time which help us to trace the existence of a particular variable during the runtime of a program.

What is the use of storage class specifiers in C?

We use the storage class in the C language for determining the visibility, lifetime, initial value, and memory location of any given variable. The storage classes define the visibility (scope) and the lifetime of any function/ variable within a C program. These classes precede the type that they are going to modify.

Why should we use register storage specifier?

The register storage class specifier indicates to the compiler that the object should be stored in a machine register. The register storage class specifier is typically specified for heavily used variables, such as a loop control variable, in the hopes of enhancing performance by minimizing access time.

What are the advantages of storage classes?

The advantage of such storage class is that since the data is in the processor itself,its access and operation on such data is faster. There is limitation on the size of the data that can declared to be register storage class. The data should be such that it doesn't require more than 4 bytes.


1 Answers

The reason is mostly historical: linkage came into the design of C language as an afterthought. In the early versions you could redeclare global variables as many times as you wish, and linker would merge all these declarations for you:

Ritchie's original intention had been to model C's rules on FORTRAN COMMON declarations, on the theory that any machine that could handle FORTRAN would be ready for C. In the common-block model, a public variable may be declared multiple times; identical declarations are merged by the linker. (source)

The current rule of a single declaration came later, along with extern keyword. At that point there was a body of C code significant enough to make backward compatibility important. That is probably the reason why language designers refrained from introducing a new keyword for handling linkage, reusing static instead.

like image 192
Sergey Kalinichenko Avatar answered Oct 03 '22 03:10

Sergey Kalinichenko