vendredi 24 juin 2016

Why the default value of automatic variable is 0 instead of garbage one?

Consider the code snippet:

int main()  
{
int n;
cout<<n;
float f;
cout.setf(ios_base::fixed,ios_base::floatfield);
cout<<endl<<f;
char ch;
cout<<endl<<static_cast<int>(ch);
return 0;
}

Output:

0
0.000000
0

Why it is not set to the garbage value instead of zero? Is it the consequence of undefined behavior? Compiled with g++4.8.2 on Ubuntu 14.04.

Aucun commentaire:

Enregistrer un commentaire