4. Variables
Identifiers
All the variables we have used so far
were named using letters a
, b
, x
, y
, z
, A
. However, variable names need not consist of a single letter. In fact, it is recommended to use names that describe what variables stand for. In C++, names can be composed of lower case and upper case letters of the English alphabet, numerals, and the symbol '_
',
called the underscore. In addition, no name may begin with a digit, and no spaces are allowed: that is, a name must be a single word.
Here are a few examples:
Valid Name | Invalid Name |
---|---|
second_factor | second factor |
row5 | 5row |
areaOfTriangle | area of triangle |
NoSpaces | No Spaces |
omega | Ω |
__icecream | ice-cream |
num_of_elements | num of elements |
PI | π |
l33t | 1337 |
Another important thing to remember is that
C++ distinguishes lowercase and uppercase letters. That is why a
and A
are two
different names. Also, friday
and Friday
are two different names. In other words, the C++ language is
case-sensitive.
Since names in C++ are used to uniquely identify a variable or some other thing, they are more properly called identifiers.