Loading, please wait...

Initialization of Variables

A variable can be initialized (given a starting value) when it is declared by setting it equal to a constant value or expression.

char letter='a';

int overtime=10;

float sigma_squared=2*1.234e-5;

Initializations can be mixed with other declarations:

int xyz=4, Marky, joey=1, ABC;

If a variable is not initialized then:

  • Global variables are initialized by default to zero.
  • Local variables declared inside main or any other function will have a random starting value. ie. There is NO default initialization for local variables. ie. They will NOT have a starting value of zero.

 

So, Initialization of local variables does not need to be to a constant.

char ch = getchar();

This will get the starting value from the keyboard.

Note also that local variables designated as static or extern have different initialization properties.