Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does typedef with no type mean?

Tags:

c

typedef

From the Standard N1570 6.7.8:

A typedef declaration does not introduce a new type, only a synonym for the type so specified.

So I expected that it is not possible to write something like this:

typedef t;
t *t_ptr;

and it should fail to compile since no type to introduce a synonym to provided. But it is fine: Demo. So what does this ever mean and why does it compile?

like image 272
Some Name Avatar asked Feb 04 '26 23:02

Some Name


2 Answers

This relies on the fact that, missing type specification defaults to int.

So, your statement

 typedef t;

is the same as

 typedef int t;

With the proper level of warning, compiler emits warning:

warning: type defaults to ‘int’ in declaration of ‘t’ [-Wimplicit-int]
 typedef t;
         ^

That said, do not rely on this behaviour, "implicit int" rule has been obsolete since C99.

like image 90
Sourav Ghosh Avatar answered Feb 06 '26 12:02

Sourav Ghosh


It defaults to an int.

The compiler warning shows what is going on:

#1 with x86-64 gcc 8.2
<source>:1:9: warning: type defaults to 'int' in declaration of 't' [-Wimplicit-int]
 typedef t;

From C99 onwards, the implicit int rule was removed. So this is not applicable from C99 onward.

If you use the -pedantic-errors compiler option in GCC (meaning strict conformity to the standard), it issues an error. See here.

If you are interested, the relevant section in C89 standard which allowed this:

3.5.2 Type specifiers
Each list of type specifiers shall be one of the following sets; the type specifiers may occur in any order, possibly intermixed with the other declaration specifiers.

  • void
  • char
  • signed char
  • unsigned char
  • short , signed short , short int , or signed short int
  • unsigned short , or unsigned short int
  • int , signed , signed int , or no type specifiers

So in C99, the last part of what was bolded above (or no type specifiers) was removed.

like image 45
P.W Avatar answered Feb 06 '26 12:02

P.W