Default value of a variable

What is the initial value of a variable before it being assigned a value?
For different variable types, I have different initial values:

  • (unsigned)int or (unsigned)long or bool(also arrays): 0
  • (unsigned)long long: 1
  • float: 0
  • double: 4.94066e-324
  • long double: 1.79581e+2383
  • (unsigned or signed)char`: (doesn’t output anything)
  • double array: 3.23796e-319
  • string:(unpredictable(random) value)

So why is default initial value like this for different var type?

1 Like

@pkien I assume that you are asking for c++/c.These languages do not assign default values to variables and only memory chunk is assigned when you write statements like: int i; called declaration statements. Whatever value was there before in the memory is taken up by the variable.
You have to assign them with values as in statements like: i=1; etc.This is called initialization.
You can achieve both tasks in a single statement as int i=1;
Also note that these value will vary on different runs/machines.Effectively all are random.

pls upvote if you find it helpful.For any queries comment below.

1 Like