I tried the following code
#include <stdio.h>
int main(void)
{
typedef static int sint;
sint i = 10;
return 0;
}
and hit the following error:
error: multiple storage classes in declaration specifiers
When I referred the C99 specification, I came to know that typedef is a storage class.
6.7.1 Storage-class specifiers
Syntax
storage-class-specifier:
typedef
extern
static
auto
register
Constraints: At most, one storage-class specifier may be
given in the declaration specifiers in a declaration
Semantics: The typedef specifier is called a ‘‘storage-class specifier’’
for syntactic convenience only;
The only explanation that I could find (based on some internet search and cross referring various sections in C99 specification) was syntactic convenience only to make the grammar simpler.
I’m looking for some justification/explanation on how can a type name have storage class specifier?
Doesn’t it make sense to have a code like typedef static int sint;?
or Where am I going wrong?!
Yes,
typedefis a storage-class-specifier as you found in the standard. In part it’s a grammatical convenience, but it is deliberate that you can either havetypedefor one of the more “obvious” storage class specifiers.A typedef declaration creates an alias for a type.
In a declaration
static int x;the type ofxisint.statichas nothing to do with the type.(Consider that if you take the address of
x,&xhas typeint*.int *y = &x;would be legal as wouldstatic int *z = &xbut this latterstaticaffects the storage class ofzand is independent of the storage class ofx.)If something like this were allowed the
staticwould have no effect as no object is being declared. The type being aliased is justint.