Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
498 views
in Technique[技术] by (71.8m points)

c - Is typedef a storage-class-specifier?

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?!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Yes, typedef is 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 have typedef or 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 of x is int. static has nothing to do with the type.

(Consider that if you take the address of x, &x has type int*. int *y = &x; would be legal as would static int *z = &x but this latter static affects the storage class of z and is independent of the storage class of x.)

If something like this were allowed the static would have no effect as no object is being declared. The type being aliased is just int.

typedef static int sint;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...