#include <iostream>
using namespace std;
int main()
{
int a;
int b;
cout<<a<<endl;
cout<<b;
}
Why b has 16 value when i compile this? I cant understand why b has 16 not 0. Int doesnt have 0 as default value?
#include <iostream>
using namespace std;
int main()
{
int a;
int b;
cout<<a<<endl;
cout<<b;
}
Why b has 16 value when i compile this? I cant understand why b has 16 not 0. Int doesnt have 0 as default value?
Absolutely not. C++ does not initialise variables unless you ask it to. (Setting a variable to 0 for example is at least one instruction: typically reg XOR reg
. That could be wasteful.) The behaviour of reading an uninitialised int
is undefined.
(Note you can do some things with uninitialised variables, such as setting a pointer or reference to one, computing sizeof(a)
, and using decltype(a)
. But passing it by value to a function is undefined behaviour. That often trips up even professional programmers.)