Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

typedef — a type alias

The typedef keyword gives a type an additional name. It creates neither a distinct type nor an object.

Version: C23; repository compiler flag, production runtime not attested

Syntax

typedef type_specifiers declarator;
/* Examples: */
typedef unsigned int Count;
typedef int *IntPtr;

typedef declares a type alias. In typedef unsigned int Count;, Count denotes unsigned int, so it provides no distinction between these types during type checking.

The alias name occupies the identifier position in a declarator. This also allows naming pointer, array, and function types. An alias follows scope rules rather than macro-style textual substitution.

Beware of const: after typedef int *IntPtr;, const IntPtr pointer = &value; declares a constant pointer to modifiable int, not a pointer to constant int. The example changes the pointed-to value while leaving the pointer unchanged.

Examples

#include <stdio.h>

typedef unsigned int Count;
typedef int *IntPtr;

int main(void)
{
    Count count = 3u;
    int value = 7;
    const IntPtr pointer = &value;
    *pointer = 9;
    printf("%u %d\n", count, value);
    return 0;
}

Result

3 9

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources