Unsigned Types

Each of the three integer types you just learned comes in an unsigned variety that can't hold negative values. This has the advantage of increasing the largest value the variable can hold. For example, if short represents the range -32768 to +32767, then the unsigned version can represent the range 0 to 65535. Of course, you should use unsigned types only for quantities that are never negative, such as populations, inventory counts, and happy face manifestations. To create unsigned versions of the basic integer types, just use the keyword unsigned to modify the declarations:

 unsigned short change; // unsigned short type unsigned int rovert; // unsigned int type unsigned quarterback; // also unsigned int unsigned long gone; // ...

Get The Waite Group's C++ Primer Plus, Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.