[Univ of Cambridge] [Dept of Engineering]
next up previous contents
Next: Operators Up: Review Previous: Keywords

Built-in Types and Enumerations

There are integral types (char, short, int, long) which can be intended to hold signed (the default) or unsigned values. So for example ``unsigned char c'' creates a one-byte variable big enough to hold any integer from 0 to 255. There are also floating point types (float, double, long double). If you want an integer variable to be restricted to containing just a few specific values you can create an enumerated type
  enum user_type {ugrad, pgrad, staff, visitor};
  user_type u;
creates an enumerated type called user_type. u can only be set to the values 0, 1, 2 or 3 (or equivalently - and preferably - ugrad, pgrad, staff or visitor). The values can be set explicitly; for example
  enum user_type {ugrad=2, pgrad=7, staff=9, visitor=3};
  user_type u;
Enumerated types can be used in most places where integer types can be, though u++ for example isn't allowed.

Tim Love
2001-07-05