Q&A
Q Why define constants at all if you can use regular variables instead?
A Constants, especially those declared using the keyword const, give you a way of telling the compiler that the value of a particular variable should be fixed and should not be allowed to change. Consequently, the compiler always ensures that the constant variable is never assigned another value—not even if another programmer picks up your work and inadvertently tries to overwrite the value. So, declaring constants where you know the value of a variable should not change is a good programming practice and increases the quality of your application.
Q Why should I initialize the value of a variable?
A If you don’t initialize, you don’t know what the variable contains for a starting value. The starting value is just the contents of the location in the memory that are reserved for the variable. For example, initialization like this:
int myFavoriteNumber = 10;
writes the initial value of your choosing—in this case 10—to the memory location reserved for the variable myFavoriteNumber as soon as it is created.
Q Why does C++ give me the option of using short int and int and long int? Why not just always use the integer that can store the highest number?
A C++ is a programming language that is used to program for a variety of applications, many running on devices with little computing capacity or memory resources. A programmer can often save memory or speed or both by choosing the right kind of variable where high values are not needed. If you are programming on a regular desktop or a high-end smart phone, chances are that the performance gained or memory saved in choosing one integer type over another is going to be insignificant; in some cases there may be no difference.
Q Why should I avoid using global variables frequently? Isn’t it true that they’re usable throughout my application, and I can save some time otherwise lost to passing values around functions?
A Global variables can be read and assigned globally. The latter is the problem as they can be changed globally. Say that you are working on a project with a few other programmers in a team. You have declared your integers and other variables to be global. If any programmer on your team changes the value of your integer inadvertently in code—which even might be a different .cpp file than the one you are using—the reliability of your code is affected. Therefore, you are advised to use global variables as infrequently as possible.
Q I am using unsigned integers that are supposed to contain only positive integer values and zero. What happens if I decrement a zero value contained in an unsigned int?
A You see a wrapping effect. Decrementing an unsigned integer that contains 0 by 1 causes the variable to wrap to the highest value it can hold. Check Table 3.1, where you can see that an unsigned short can contain values from 0 to 65,535. When you declare an unsigned short instantiated to 0 and decrement it, expect it to contain 65,535!
unsigned short myShortInt = 0; // Initial Value myShortInt = myShortInt - 1; // Decrement by 1 std::cout << myShortInt << std::endl; // Output: 65535!
Note that this is not a problem with short; it is only a problem with an unsigned short. An unsigned integral type is not to be used when negative values are within the specifications.