C++ Data Type Usage

The Going Overboard section on page 43 of C++ All-In-One for Dummies, 3rd Edition talks about the problems that can occur when you try to stuff a number that’s too large into a specific data type. The problem with the example shown:

    cout << 8762547892451 * 10 / 2 * 3 + 25 << endl;

is that it doesn’t actually result in an error. C++ accepts the large number by using a data type that can hold it automatically, rather than using a default data type of long as would have happened in the past. It’s nice that C++ automatically fixes ambiguous code for you, but it also means that the example doesn’t work as described in the book. In order to see the example as originally intended, you need to change the code to read:

    long MyLong = 8762547892451 * 10 / 2 * 3 + 25;
    cout << MyLong << endl;

The code will now produce an error, just as described in the book, because the data type isn’t ambiguous any longer. The error message does differ slightly. What you’ll see is an error message of:

warning: overflow in implicit constant conversion [-Woverflow]

Except for having to make the code less ambiguous, the section should continue to work as it did before. Please let me know if you have any questions or concerns about this example at John@JohnMuellerBooks.com.