Search code examples
cint

Define integer (int); What's the default value?


int i;
int data[5] = {0};
data[0] = i;

What's the value in data[0]?

Also, what's the meaning of this line?

if (!data[0]) { ... }

Solution

  • In most cases, there is no "default" value for an int object.

    If you declare int i; as a (non-static) local variable inside of a function, it has an indeterminate value. It is uninitialized and you can't use it until you write a valid value to it.

    It's a good habit to get into to explicitly initialize any object when you declare it.