Skip to content
  • typedef · C

typedef

LearnViaNotes @learnvianotes· 2 min read

typedef creates an alias for an existing type — shorter names, clearer intent and a single place to change a type. It never creates a new, distinct type.

C
#include <stdio.h>
#include <stdint.h>

typedef unsigned long ulong;                 /* shorter name */
typedef int32_t Meters;                      /* documents intent (still an int32_t) */
typedef struct { double re, im; } Complex;   /* anonymous struct, used without 'struct' */
typedef int (*Compare)(int, int);            /* function pointer type */
typedef char Name[32];                       /* array type */

int ascending(int a, int b) { return a - b; }

int main(void) {
    ulong big = 123456789UL;
    Meters height = 8849;
    Complex z = {1.5, -2.0};
    Compare cmp = ascending;
    Name who = "Tenzing";
    printf("%lu %d %.1f%+.1fi %d %s %zu\n", big, height, z.re, z.im, cmp(3, 1), who, sizeof(Name));
    return 0;
}
Output
123456789 8849 1.5-2.0i 2 Tenzing 32

typedef struct patterns

C
#include <stdio.h>
#include <stdlib.h>

/* tag + typedef: the tag is needed for the self-reference inside the body */
typedef struct Node {
    int value;
    struct Node *next;
} Node;

int main(void) {
    Node second = {2, NULL};
    Node first = {1, &second};
    struct Node *same = &first;          /* both names work */
    printf("%d %d\n", same->value, first.next->value);
    return 0;
}
Output
1 2

typedef vs #define

C
#include <stdio.h>

#define PTR_D int *
typedef int *PtrT;

int main(void) {
    int x = 1, y = 2;
    PTR_D a, b;       /* expands to: int *a, b;  -> b is an int! */
    PtrT  c, d;       /* both are int *                         */
    a = &x; b = y;
    c = &x; d = &y;
    printf("%zu %zu %zu %zu\n", sizeof a, sizeof b, sizeof c, sizeof d);
    return 0;
}
Output
8 4 8 8

Does typedef create a new type?

Free for students and teachers

Write notes like this one

Cards for every idea, quizzes and flashcards, read aloud — on the web and your phone.

Something wrong with this note? Open it to report