3

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?

dbush
  • 205,898
  • 23
  • 218
  • 273
Some Name
  • 8,555
  • 5
  • 27
  • 77

3 Answers3

6

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.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
3

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.

P.W
  • 26,289
  • 6
  • 39
  • 76
0

A typedef declaration defines a synonym for an object or pointer type. So you should specify both the type for which you want to create a synonym, and a name to use as synonym.

For example:

// 'byte_t' is a synonym for 'unsigned char'
typedef unsigned char byte_t;
// 'handler_t' is a synonym for 'void (*)(int)', a function pointer
typedef void (*handler_t)(int);
// 'short_p' is a synonym for 'short *'
typedef short * short_p;
// 'record' is a synonym for an anonymous structure
typedef struct {
    int a;
    char *b;
    } record;
// 'error_p' is a synonym for a pointer to 'struct error', defined somewhere else
typedef struct error *error_p;

Many more examples in the source you cited.

A. Semat
  • 58
  • 4
  • 1
    This is fine, but how does this answers the question? – Sourav Ghosh Nov 28 '18 at 05:55
  • @Sourav Ghosh I felt that the `typedef` syntax was new to him, so I give him some basic examples with comments. Other answers already addressed the compilation problem in itself. – A. Semat Nov 28 '18 at 06:07
  • Please don't invent your own terms and grammar that don't even exist in the C language. There are no syntax items called "base-type" or "type-alias". – Lundin Nov 28 '18 at 09:53
  • @Lundin. Thanks. I should have specified that `base-type` and `type-alias` should have been replaced by actual types and names. Moreover that pseudo-syntax didn't suit function types. I also changed `alias` to `synonym`, since the C standards refers to typedef names as such. – A. Semat Nov 28 '18 at 10:29