Home > Articles > Programming > C/C++

  • Print
  • + Share This
This chapter is from the book

Types of Variables and Valid Names

Integers come in two varieties: signed and unsigned. The idea here is that sometimes you need negative numbers, and sometimes you don't. Integers (short and long) that aren't labeled unsigned are assumed to be signed. signed integers are either negative or positive. unsigned integers are always positive.

Use int for Number Variables

For most programs, most of the time, you can simply declare your simple number variables as ints—these are signed integers.

Non-Integer Variable Types

Several variable types are built into C++. They can be conveniently divided into integer variables (the type discussed so far), character variables (usually char), and floating-point variables (float and double).

Floating-Point Variables

Floating-point variables can have fractional values and decimal points, unlike integers.

The types of variables used in C++ programs are described in Table 3.1. This table shows the variable types, how much room they typically take in memory, and what ranges of values can be stored in these variables. The variable type determines the values that can be stored, so check your output from Listing 3.1.

Note that the e in 3.4e38 (the number at the high end of the range of values for float) means "times ten to the power of," so the expression should be read "3.4 times ten to the 38th power," which is 340,000,000,000,000,000,000,000,000,000,000,000,000.

Table 3.1 Variable Types




unsigned short int

2 bytes

0 to 65,535

short int

2 bytes

–32,768 to 32,767

unsigned long int

4 bytes

0 to 4,294,967,295

long int

4 bytes

–2,147,483,648 to 2,147,483,647


1 byte

256 character values


1 byte

true or false


4 bytes

1.2e–38 to 3.4e38


8 bytes

2.2e–308 to 1.8e308


String variables are a special case. They are called arrays and will be discussed in more detail later.

Case Sensitivity

C++ is case sensitive. This means that words with different combinations of uppercase and lowercase letters are considered different words. A variable named age is not the same variable as Age or AGE.


This is the most common error made by programmers when reading or writing C++. Be careful.


Some words are reserved by C++, and you may not use them as variable names. These are keywords used by the compiler to understand your program. Keywords include if, while, for, and main. Your compiler manual should provide a complete list, but generally, any reasonable name for a variable is almost certainly not a keyword. See the inside back cover of this book for a list of some C++ keywords.

  • + Share This
  • 🔖 Save To Your Account