Before a variable can be used in a program, it must be declared. The form for declaring a variable is as follows:
Here, data-type is a data type of the variable and identifier is the name of the variable.
A declaration statement defines the type of the variable and therefore associates it with a set of values and operators. For a few declaration examples, consider:
int num1; float num2; char letter;
The above code segment would declare num1 as being type int, num2 as being type float, and letter as being type char. From this point forward in the program, num1 must store integer values, num2 must store floating point values, and letter must store a single character value.
Initialization is different from declaration because the variable is declared and also given an initial value. Initialization takes the following form:
data-type identifier = value;
Here, data-type is one of the data types described above, identifier is the name of the variable, and value is the variable’s initial value. For a few initialization examples, consider:
int num1 = 123; float num2 = 56.78; char letter = 'a';
The above code segment would define num1 as type int with an initial value of 123, num2 of type float with an initial value of 56.78, and letter of type char with an initial value of ‘a’.
A constant variable must be initialized and can never change. The form for named constants is:
const data-type identifier = value;
Here, value is a literal.
With named constants, initializing is the same except for the preceding const keyword. By defining a variable as const, the variable’s value cannot change during program execution. If the program attempts to change its value, an error will occur (which should be caught by the compiler).
Constants are commonly used for sharing the same data for different elements in a program. Utilizing constants appropriately can increase readability and make updating code significantly easier. For an example, consider:
const int MAXSIZE = 450;
The variable MAXSIZE may be used for a text field that is restricted to having 450 characters. Why should we use a constant? Assume we are building a user interface for some type of data entry program, and we have twenty text fields (strung throughout 10 different source files). Each text field could have a maximum character size of 450.
Now, we could specify each text field’s maximum size using a constant as follows:
Or, we could “hard code” the size for each text field as follows:
Here’s the catch: our requirements suddenly change and all text fields must now have a maximum size of 250. Using the named constant, we only have to change one line of code. If we used the “hard code” approach, we would have to change twenty lines of code (and also worry about finding them!). Whenever you change software, you should strive to make minimal changes.
For an example of the different data types as well as variable declarations and variable initializations, compile and run the following complete program:
In the next article, we’ll cover assignment statements (although you’ve already seen them). Read on for more…
Recommended next reading: Assignment