britbeef,
I offer my apology. It may be a little late for this reply to help you because of the time lag between your question and my reply. Since I work and go to school, I tend to put my independent study of C++ on the back burner when I am not between semesters.
Your question made me put my thinking cap on because the answer deals with both fundamental concepts and their application. I will address
long only. The same principle applies to
unsigned.
To begin with, C++ is a strongly typed language, and you don't want the compiler to assume what you mean when you type code.
Long is a keyword which performs two functions. The type of data which is acceptable for the variable is defined by the keyword
long. The keyword also allocates memory to store the variable's value. This determines the range of values which will fit in the variable. Too large a number will not fit because it is out of the range. The suffix
L tells the compiler the same thing as the keyword
long.
Neglecting a discussion of variable names and their values would fail to explain the significance of the seemingly insignificant redundancy of using both
long and
L. A variable's name is a container of sorts. It's value is the contents of the container. The equal (=) sign in math says that both sides are equal in value. It is normally read from left to right. In C++ this is not the case. In your example, long BigNumber = 1000000L, is a right to left operation. The value (1000000L) is placed into the variable (BigNumber). This concept is essential to the understanding of the C++ language and many other programming languages.
There are times when a value is transferred to another variable or is included in a computation. Some operations like bitwise shift, type conversions in assignments, and defining synonyms for data types must be in a form which the compiler recognizes by type.
To simplify the explanation, let's say you have some code like this:
Code:
long BigNumber = 1000000L
lots of code
answer = BigNumber
What happens when the last line is executed? The value of BigNumber (1000000L) is placed in answer. The suffix
L tells the compiler that the value is a long integer. (Also read the section in the book where calculations of mixed types (adding a float to an int, for example) are discussed.
If you acquire the habit of using the seemingly redundant syntax, you will be much less likely to encounter logical errors (where the program compiles and runs, but produces the wrong results) in your programs.
regards,
drpepper