`typedef` — C++ Keyword

`typedef` — C++ Keyword

The typedef keyword in C++: creates a type alias using the traditional C syntax.

How to use this reference page

Use reference pages to confirm names, categories, nearby facilities, and the constraints that matter before writing or reviewing code.

  • Scan the top of the page first to identify the primary types, functions, or algorithm families involved.
  • Use the nearby-page links when your question is really about a companion header, related algorithm family, or broader subsystem.
  • Validate tricky behavior with a small compileable example before relying on memory for details like invalidation, ordering, allocation, or lifetime rules.

typedef

Creates an alias for a type using the legacy C syntax. Prefer using (C++11) for new code, as using is more readable for complex types and supports templates.

Syntax

typedef existing-type alias-name;
typedef return-type (*alias-name)(parameter-types);   // function pointer alias

Example

#include <print>
#include <vector>

// Simple type alias
typedef unsigned long ulong;

// Function pointer alias
typedef int (*BinaryOp)(int, int);

// Struct with typedef (common C pattern)
typedef struct { int x; int y; } Point;

int add(int a, int b) { return a + b; }

int main() {
    ulong n = 1'000'000UL;
    std::println("{}", n);

    BinaryOp op = add;
    std::println("{}", op(3, 4));   // 7

    Point p = {1, 2};
    std::println("{} {}", p.x, p.y);
}

Notes

Example in practice

int main() {
    // Pick one facility from this reference page.
    // Write the smallest program that exercises its main precondition,
    // complexity rule, or lifetime constraint before scaling up.
    return 0;
}