Game Development Reference
In-Depth Information
This declaration tells the compiler that we would like to have an integer with the name
numberOfObjects ; it does not tell the compiler what the variable contains or what it should be
used for.
Defining Variables
We can define our variable once we have declared it. Defining a variable involves telling the compiler
what its initial value should be. This is an example of a variable definition:
numberOfObjects = 0;
It's fairly common that we will define and declare some variables at the same time. Combining the
declaration and definition would resemble this line:
int numberOfObjects = 0;
We'll be looking at the places where it is appropriate to declare names to the compiler compared to
defining them as we move through this topic. The rules for declaring and defining different variables
and types depend on the context in which they are being used.
Integers
The first set of types that we will be looking at in C++ is the integer types. The previous sections
have already shown how to declare and define an integer variable. To recap:
int numberOfObjects = 0;
This line of code declares and defines an integer variable. What this means to the compiler is that we
want to create a variable that is capable of storing whole numbers. Examples of integers are -1000,
0, 24, and 1345219. Integers can contain both positive and negative numbers.
The type int is just one of several different types that can store integers. The others are char , short ,
and long . Table 2-1 shows the different ranges of values that can be stored in these types when
using MSVC.
Table 2-1. The MSVC Integral Types, with Minimum and Maximum Values
Type Name
Number of Bytes
Minimum Value
Maximum Value
char
1
-128
127
short
2
-32,768
32,767
int
4
-2,147,483,648
2,147,483,647
long
4
-2,147,483,648
2,147,483,647
long long
8
-9,223,372,036,854,775,808
9,223,372,036,854,775,807
 
 
Search WWH ::




Custom Search