Prasoon Saurav
Prasoon Saurav

Reputation: 92864

typedef and non-simple type specifiers

Why is this code invalid?

typedef int INT;
unsigned INT a=6;

whereas the following code is valid

typedef int INT;
static INT a=1; 

?

As per my understanding unsigned int is not a "simple type specifier" and so the code is ill-formed. I am not sure though.

Can anyone point to the relevant section of the Standard which makes the first code invalid(and the second code valid)?

EDIT

Although Johannes Schaub's answer seemed to be correct and to the point(he had deleted his answer BTW) I accepted James Curran's answer for its correctness and preciseness.

Upvotes: 18

Views: 579

Answers (3)

James Curran
James Curran

Reputation: 103505

typedefs are not like macros. They are not just text substitution. A Typedef creates a new typename.

Now when you say unsigned int, the unsigned isn't a modifier which is tacked onto the int. unsigned int is the complete typename; it just happens to have a space in it.

So, when you say typedef int INT; then INT is the complete typename. It can't be modified.

static (like const) is a storage class specifier. It's not actually part of the type name.

Upvotes: 26

ShinTakezou
ShinTakezou

Reputation: 9671

Don't forget that typedef-ing is not like macro-defining; in your example, it seems like if you think your INT should be seen like a literal int. From the compiler point of view, typedef defines type-aliases, but this is not seen at "syntax" level (typedef-ed types are like "native" types at the syntax level); and since at that level unsigned is allowed before char long short or int only, your unsigned INT is seen like a "type" ("different" from char, long, short, int) preceded by unsigned.

Upvotes: 2

Scharron
Scharron

Reputation: 17757

  • 7.1.1 : static is a storage class specifier. It can be placed before any type.
  • 7.1.5 : what is a type specifier (unsigned can be combined with char, long, short, or int)

Upvotes: 11

Related Questions