Typedef declaration

From cppreference.com
< c‎ | language

The typedef declaration provides a way to create a synonym that can be used anywhere in place of a (possibly complex) type name.

Contents

[edit] Syntax

typedef type-specifier typedef-name;

[edit] Explanation

A type definition is a declaration whose storage-class specifier is typedef. Each declarator in the declaration defines an identifier to be a typedef name that is an alias for the type specified. The type specified can be another typedef name. A typedef declaration does not introduce a new type. Since typedef is a storage-class specifier, a typedef declaration may not use another storage-class specifier (e.g. static).

Objects defined with the same typedef name have a type compatible with the type specified in the typedef declaration.

When a typedef name denotes a variable length array type, the length of the array is fixed when the typedef name is defined, not when the typedef name is used.

[edit] Keywords

typedef

[edit] Example

Uppercase visually emphasizes the synonyms in the examples. Uppercase is not required.

#include <stdio.h>
 
int main(void)
{
    /* Parameterizes a C type.                                                   */
    /* When the program requires a larger f-p format, simply change the typedef. */
    typedef float REAL;
    /* Variables x1 and x2 have the same type.                                   */
    REAL x1;
    float x2;
    printf("sizeof(x1) = %zu\n", sizeof(x1));   // 4 bytes
    printf("sizeof(x2) = %zu\n", sizeof(x2));   // 4 bytes
    const REAL y = 0.0;
    static REAL z;
 
//  typedef float REAL;   /* error: redefinition of typedef 'REAL' [-Wpedantic] */
//  typedef double REAL;  /* error: conflicting types for 'REAL'                */
 
    /* Shortens a lengthy C type name. */
    typedef unsigned long int ULONG;
 
    /* Provides a name indicating what a variable represents.              */
    /* Both synonyms are floats but represent different physical concepts. */
    typedef float TEMP;
    typedef float WIND_SPEED;
 
    /* Provides a common C idiom to avoid writing "struct" repeatedly. */
    /* anonymous struct                                                */
    typedef struct {int a; int b;} S, *PS;
    /* Variables ps1 and ps2 have the same type.                       */
    PS ps1;
    S* ps2;
 
    /* Provides a common C idiom to avoid writing "union" repeatedly. */
    /* anonymous union                                                */
    typedef union {int i; float f;} U, *PU;
    /* Variables pu1 and pu2 have the same type.                      */
    PU pu1;
    U* pu2;
 
    /* Provides a common C idiom to avoid writing "enum" repeatedly. */
    /* anonymous enum                                                */
    typedef enum {club,diamond,heart,spade} E, *PE;
    /* Variables pe1 and pe2 have the same type.                     */
    PE pe1;
    E* pe2;
 
    /* typedef with array */
    typedef int ARR_T[10];
    ARR_T a1;
    int a2[10];   /* same as a1 */
    printf("sizeof(a1) = %zu\n", sizeof(a1));   // 40 bytes
    printf("sizeof(a2) = %zu\n", sizeof(a2));   // 40 bytes
 
    /* typedef with pointer */
    int i=0;
    typedef int * PTR_TO_INT;
    PTR_TO_INT pi = &i;
 
    /* typedef with function pointer                                                */
    /* FP is a synonym for a pointer to a function that neither takes nor returns a */
    /* value.                                                                       */
    typedef void (*FP)(void);
 
    /* error: multiple storage classes in declaration specifiers */
    /* typedef static unsigned int uint;                         */
 
    return 0;
}

Possible output:

sizeof(x1) = 4
sizeof(x2) = 4
sizeof(a1) = 40
sizeof(a2) = 40

[edit] See also

C++ documentation for Typedef declaration