Default values

Variables of built-in types should be initialized before you first use them, but there are some situations when the compiler will provide a default value.

If you declare a variable at file scope, or globally in your project, and you do not give it an initial value, the compiler will give it a default value. For example:

    int outside;      int main()     {         outside++;         cout << outside << endl;     }

This code will compile and run, printing a value of 1; the compiler has initialized outside to 0, which is then incremented to 1. The following code will not compile:

    int main()     {         int inside;         inside++;         cout << inside << endl;     }

The compiler will complain that the increment operator is being used on an uninitialized variable.

In the ...

Get Beginning C++ Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.