Can we place "typedef" specifier anywhere in the declaration? [duplicate]
Asked Answered
S

2

16

The syntax of typedef specifier :

typedef <existing_name> <alias_name>

for example:

typedef long unsigned int Int;

It's working fine.

But, If I place typedef anywhere in the declaration, Like this:

long unsigned typedef int Int;

Then, It's also working fine.

Why? Can we place typedef anywhere in the declaration?

Shing answered 18/8, 2017 at 7:13 Comment(6)
why not? int short static someVar; is also valid, right?Assuan
Because the C standard specifies that. Read n1570Narcoma
Good but no good, you could have googled something like "typedef declaration ordering". -Edit NVM you need to know typedef is a "storage-class" to find the dupe effectively.Conklin
C standard says that on a syntactic point of view typedef is just a storage class specifier like extern or static, so it can be intermixed with the type declarator.Gauze
@rsp Please do not put language tags on question title, there are tags fields dedicated for that purpose.Assuan
Similar to this questionMathilda
A
17

First of all, quoting from §6.11.5, "Future language directions"

1 The placement of a storage-class specifier other than at the beginning of the declaration specifiers in a declaration is an obsolescent feature.

So, do not rely on this, as it may be removed in the future.


That said, to understand why this works, check the C11 standard, chapter §6.7.2:

[...] the type specifiers may occur in any order, possibly intermixed with the other declaration specifiers.

From §6.7.1, we know typedef is a storage-class specifier (one particular sort of a declaration specifier), so it can be placed after (or before) the type specifier (i.e., can be intermixed). It does not change anything.

Assuan answered 18/8, 2017 at 7:20 Comment(0)
L
7

This is indeed allowed by the C standard. typedef is a storage class specifier and if you look at the grammar given in the C standard (N1570, latest draft for C11, §6.7 p1):

Syntax
declaration:
. declaration-specifiers init-declarator-list(opt) ;
. static_assert-declaration
declaration-specifiers:
. storage-class-specifier declaration-specifiers(opt)
. type-specifier declaration-specifiers(opt)
. type-qualifier declaration-specifiers(opt)
. function-specifier declaration-specifiers(opt)
. alignment-specifier declaration-specifiers(opt)
init-declarator-list:
. init-declarator
. init-declarator-list , init-declarator
init-declarator:
. declarator
. declarator = initializer

the storage class specifier can appear after other declaration specifiers like the type specifier.

But you shouldn't use it, it's obsolescent, see §6.11.5:

The placement of a storage-class specifier other than at the beginning of the declaration specifiers in a declaration is an obsolescent feature.

Lakendra answered 18/8, 2017 at 7:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.