Question:
Why do uninitialized objects of built-in type defined inside a function body have undefined value, while objects of built-in type defined outside of any function are initialized to 0
or ''
?
Take this example:
#include <iostream>
using std::cout; using std::endl;
int ia[10]; /* ia has global scope */
int main()
{
int ia2[10]; /* ia2 has block scope */
for (const auto& i : ia)
cout << i << " "; /* Result: 0 0 0 0 0 0 0 0 0 0 */
cout << endl;
for (const auto& i : ia2)
cout << i << " "; /* Result: 1972896424 2686716 1972303058 8
1972310414 1972310370 1076588592 0 0 0 */
return 0;
}