Home

typedef

Typedef is a language feature in C and C++ that introduces an alias name for an existing type. It does not create a new type, but it provides another name for the same type, improving readability and portability.

Syntax: typedef existing_type new_name; Example: typedef unsigned long ulong; This allows you to use ulong in

Structs: In C, you often pair typedef with struct to avoid writing struct each time. Example: typedef

C++ considerations: In C++, typedef exists but the using alias declaration is preferred since C++11. For example,

Notes and limitations: Typedef creates an alias, not a distinct type or new type identity. It respects

place
of
unsigned
long.
You
can
also
typedef
pointer
types,
arrays,
and
function
pointer
types,
e.g.,
typedef
int
*IntPtr;
typedef
int
(*CompareFn)(const
void*,
const
void*).
struct
Point
{
int
x;
int
y;
}
Point;
After
this
declaration,
you
can
declare
variables
simply
as
Point
p;
without
the
struct
keyword.
using
Vec
=
std::vector<int>;
is
functionally
equivalent
to
typedef
std::vector<int>
Vec;
but
the
using
form
is
often
clearer,
especially
for
templates.
scope
like
any
other
declaration.
It
cannot
introduce
new
template
parameters
or
modify
type
behavior
beyond
providing
an
alternate
name.
When
used
with
qualifiers
like
const
or
volatile,
the
qualifiers
apply
to
the
underlying
type
in
the
alias,
which
can
affect
how
the
alias
interacts
with
pointers
and
references.