Home > Articles > Programming > Windows Programming

Oh, So That's How C++ Variables and Constants Work!

  • Print
  • + Share This
Variables are the building blocks of C coding, and in this chapter by Jesse Liberty and David Horvath, you'll learn how to manipulate, define, and display variables in C++.
This chapter is from the book

Hour 3: Variables and Constants

What You'll Learn in This Hour:

  • How to declare and define variables and constants
  • How to assign values to variables and manipulate those values
  • How to write the value of a variable to the screen

What Is a Variable?





Figure 3.1Figure 3.1 A visual representation of memory.

What Is RAM?


Setting Aside Memory

When you define a variable in C++, you must tell the compiler not only what its name is, but also what kind of information it will hold: integer, character, and so forth. This is the variable's type. Another word for type that you may see is datatype. The type of the variable tells the compiler how much room to set aside in memory to hold the variable's value.


Size of Integers

A char variable (used to hold characters) is most often one byte long. A short int is two bytes on most computers; a long int is usually four bytes, and an int (without the keyword short or long) can be two or four bytes. If you are running Windows 95/98, Windows XP, or Windows NT/2000/2003, you can count on your int being four bytes as long as you use a modern compiler.

But you should not count on that being true on your system—check. You are assured that the size of a short int will be smaller than or the same as the size of an int, and that the size of an int will be the same or smaller than the size of a long. Floating point data types are another story entirely and will be discussed shortly.


Listing 3.1 -sizer.cpp Determines the Size of Variable Types on Your Computer

 0: #include <iostream>
 2: int main()
 3: {
 12:   std::cout << "The size of a bool is:\t\t";
 13:   std::cout << sizeof(bool) << " bytes.\n";
 19:   return 0;
 20: }


The size of an int is     4 bytes.
The size of a short int is   2 bytes.
The size of a long int is   4 bytes.
The size of a char is     1 bytes.
The size of a bool is     1 bytes.
The size of a float is     4 bytes.
The size of a double is    8 bytes.

Your Results May Vary




signed and unsigned


Because you have the same number of bytes (and therefore the number of bits) 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.

Bit for signed and unsigned Integers

For signed integers, one bit is used to handle the sign itself; for unsigned that bit is used for more numbers. If you counted all the possible numbers available in signed and unsigned, you would find that you have the same count. The difference is how they are represented!

Fundamental Variable Types





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


4 bytes

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

unsigned int

4 bytes

0 to 4,294,967,295


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

Although many consider it poor programming practice, you can use a char variable as a very small integer.

  • + Share This
  • 🔖 Save To Your Account