Variable Declarations

To declare a variable, specify a data type and name. Optionally you may provide an initializer.

  • data-type variable-name ['=' expression];

Details

Details are as follows:

  • You can declare more than one variable in the same declaration.
  • Initialization is optional.
  • Without initialization, integer variables are initialized to zero.
  • Without initialization, double variables are initialized to zero.
  • Without initialization, string variables are initialized to empty.
  • Without initialization, Boolean variables are initialized to false.

Examples

The following example declares an integer variable named counter.

int counter;

The following example declares several integer variables named x, y, and z.

int x,y,z;

The following example declares an integer variable named counter and initializes its value to zero.

int counter = 0;

The following example declares a double-precision variable named acceleration and initializes its value to 9.80665.

double acceleration = 9.80665;

The following example declares a string variable named nameprefix and initializes its value to Quadrant.


string nameprefix = "Quadrant";