|

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, avoid such horrific
names as J23qrsnf, and restrict single-letter variable
names (such as x or i) to variables that are used
only very briefly. Try to use expressive names such
as myAge or howMany. Such names are easier to understand
after completing this book when you are scratching
your head trying to figure out what you meant when
you wrote that line of 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;
ULONG
z;
z
= x * y;
}
Example
2
main
()
{
unsigned
short Width;
unsigned
short Length;
unsigned
short Area;
Area
= Width * Length;
}
Clearly,
the second program is easier to understand, and
the inconvenience of having to type the longer variable
names is more than made up for by how much easier
it is to maintain the second program.
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.
NOTE: 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.
There
are various conventions for how to name variables,
and although it doesn’t much matter which method
you adopt, it is important to be consistent throughout
your program.
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 camel’s hump.
Some
people find the underscore character (my_car) to
be easier to read, while others prefer to avoid
the underscore, because it is more difficult to
type. This book uses camel-notation, in which the
second and all subsequent words are capitalized:
myCar, theQuickBrownFox, and so forth.
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
define a variable by writing the type, then the
variable name. DO use meaningful variable
names. DO remember that C++ is case sensitive. DON’T
use C++ keywords as variable names. DO understand
the number of bytes each variable type consumes
in memory, and what values can be stored in variables
of that type. DON’T use unsigned variables
for negative numbers.
|