Answer the question
In order to leave comments, you need to log in
Why does an uninitialized variable have a value of 0?
I am reading a book on C++ Primer 5th edition (Stanley Lippman, Josée Lajoie, and Barbara E. Moo).
It says that if a variable is declared outside the function, then it is automatically initialized with the value 0, and if the variable is declared inside the function, then it will not be initialized and will contain the value undefined. This is about build-in types.
#include <iostream>
using namespace std;
int a;
int main(){
int b;
cout << a << " " << b << endl;
}
0 0?
Answer the question
In order to leave comments, you need to log in
Because that's how the stars are aligned. Potentially, the output can be anything, which is why it is undefined. Zero - included in many different options
In c++ there is no undefined, there is only NULL the same 0 or nullptr.
There is no special meaning of undefined. This means that the compiler can do whatever it wants with it. In 2008 studio output 0 1 for example.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question