Previous Table of Contents Next


signed and unsigned

In addition, all of these types come in two varieties: signed and unsigned. Sometimes you need negative numbers, and sometimes you don’t. Integers (short and long) without the word “unsigned” are assumed to be signed. signed integers are either negative or positive. unsigned integers are always positive.

Because you have the same number of bytes for both signed and unsigned integers, the largest number you can store in an unsigned integer is twice as big as the largest positive number you can store in a signed integer. An unsigned short integer can handle numbers from 0 to 65,535. Half the numbers represented by a signed short are negative, thus a signed short can only represent numbers from –32,768 to 32,767.

Fundamental Variable Types

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

Floating-point variables have values that can be expressed as fractions—that is, they are real numbers. Character variables hold a single byte and are used for holding the 256 characters and symbols of the ASCII and extended ASCII character sets.

The ASCII character set is the set of characters standardized for use on computers. ASCII is an acronym for American Standard Code for Information Interchange. Nearly every computer operating system supports ASCII, though many support other international character sets as well.

The types of variables used in C++ programs are described in Table 3.1. This table shows the variable type, how much room this book assumes it takes in memory, and what kinds of values can be stored in these variables. The values that can be stored are determined by the size of the variable types, so check your output from Listing 3.1.

Table 3.1 Variable Types

Type Size Values

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
char 1 byte 256 character values
bool 1 byte true or false
float 4 bytes 1.2e–38 to 3.4e38
double 8 bytes 2.2e–308 to 1.8e308

Defining a Variable

You create, or define, a variable by stating its type, followed by one or more spaces, followed by the variable name and a semicolon. The variable name can be virtually any combination of letters, but cannot contain spaces. Legal variable names include x, J23qrsnf, and myAge. Good variable names tell you what the variables are for; using good names makes it easier to understand the flow of your program. The following statement defines an integer variable called myAge:

int myAge;

As a general programming practice, try to use names that tell you what the variable is for. Names such as myAge or howMany are much easier to understand and remember than names like xJ4 or theInt. If you use good variable names, you’ll need fewer comments to make sense of your code.

Try this experiment: Guess what these pieces of programs do, based on the first few lines of code:

Example 1

main()
{
     unsigned short x;
     unsigned short y;
     unsigned int z;
     z = x * y;
}

Example 2

main ()
{
     unsigned short Width;
     unsigned short Length;
     unsigned short Area;
     Area = Width * Length;
}

Case Sensitivity

C++ is case sensitive. In other words, uppercase and lowercase letters are considered to be different. A variable named age is different from Age, which is different from AGE.

Some compilers allow you to turn case sensitivity off. Don’t be tempted to do this; your programs won’t work with other compilers, and other C++ programmers will be very confused by your code.

Many programmers prefer to use all lowercase letters for their variable names. If the name requires two words (for example, my car), there are two popular conventions: my_car or myCar. The latter form is called camel-notation, because the capitalization looks something like a hump.

Keywords

Some words are reserved by C++, and you may not use them as variable names. These are keywords used by the compiler to control 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.

Do Don’t
DO define a variable by writing the type, then the variable name. DON’T use unsigned variables for negative numbers.
DO use meaningful variable names. DON’T use C++ keywords as variable names.
DO remember that C++ is case sensitive.
DO understand the number of bytes each variable type consumes in memory, and what values can be stored in variables of that type.


Previous Table of Contents Next