Advertisement

Typedefs

  • Mikael Olsson
Chapter

Abstract

An alias for a type can be created using the typedef keyword followed by the type and alias name. By convention, uppercase letters are commonly used for these definitions.

typedef unsigned char BYTE;

An alias for a type can be created using the typedef keyword followed by the type and alias name. By convention, uppercase letters are commonly used for these definitions.

typedef unsigned char BYTE;

Once defined, the alias can be used as a synonym for its specified type.

BYTE b; /* unsigned char */

typedef does not only work for existing types, but can also include a definition of a user-defined type—such as a struct, union, or enum. This can make a complex type easier to understand.

typedef struct { int points; } score;
score a, b, c;
a.points = 10;

If used properly, a type alias can simplify a long or confusing type name, making the code easier to understand. Another benefit they provide is the ability to change the definition of a type from a single location, which can help make a program more portable.

Copyright information

© Mikael Olsson 2019

Authors and Affiliations

  • Mikael Olsson
    • 1
  1. 1.Hammarland, Länsi-SuomiFinland

Personalised recommendations