Chapter 3 - Storing Data: Variables and Data Types in C++
This chapter presents how C++ language store and use data into the program. It presents the concepts behind different data types and ways to use them.
Variable
Variables are temporary storage location of data. A variable is a named data storage location in the computer's memory. By using a variable's name in the program, it is in effect, referring to the data stored there.
There are different data that can be stored in variable like the name of person, the age of person, the daily and annual salary, and many others. These data has several types and table 3.1 presents the list of data types. These data types are called primitive or built-in data types as they are provided by the C++ language.
Table 3.1
Note: The sizes of variables in other computers might be different from those shown in the above table, depending on the compiler and the computer being use.
In table 3.1, notice that different data types corresponds to different range of values they can store. For example the data type character can store values from -128 to 127 and the data type integer can store values from -2147483648 to 2147483647. However, integer data type requires 4 bytes in the computer's memory where on the other hand the data type character only requires 1 byte.
Based from table 3.1, a programmer needs to determine the appropriate data type for the needed data, because it is essential to save memory space. For example, the best data type for a person's age would be the unsigned char. A person's age can be any number between 0 and up, therefore choosing unsigned char with the range of 0-255 is the best choice considering that there's no more person alive beyond the age of 255, and also this data type only requires 1 byte.
What about the person's name, which data type should be used? In table 3.1, under the range column, there are no value that represents the alphabet, because alphabet characters are stored in the computer's memory in form of binary numbers, hence the range is definitely in numeric value. However, each character including the alphabet has an equivalent numeric value which corresponds to the 255 characters found in the keyboard including the alphabet and special characters like ! ? $. The data type used for these characters is the data type Character (char).
Variable Declaration and definition
Before a variable can be used in a C++ program, it must be declared. A variable declaration tells the compiler the name and data type of a variable and can be optionally initialize to a specific value. Variable declaration allocates a space in the computer's memory where the data will reside. If the program attempts to use a variable that hasn't been declared, the compiler generates an error message.
Syntax for variable declaration:
<data-type> <variable-name>;
In the syntax, the <variable-name> should be replaced with any user-defined name, and the <data-type> must be replaced with the appropriate data type as listed in table 3.1
Example of variable declaration:
unsigned char age;
The example declares a variable named age with the data type of unsigned char. Hence, the age can now store unsigned char value. In giving variable names in C language, variable names must adhere to the following rules:
1. The name can contain letters, digits, and the underscore character _ ;
2. The first character of the name must be a letter. The underscore is also a legal first character, but it’s not recommended;
3. Case sensitive. A is different from a; and
4. C++ keywords or reserved words can't be used as variable names. A keyword is a word that has special meaning in C++ language . Table 3.3 at the last section of this chapter presents the lists of C++ keywords.
The following list in table 3.2 contains some examples of legal and illegal C++ variable names:
Table 3.2
For many compilers, a C++ variable name can be up to 31 characters long. (It can actually be longer than that, but the compiler looks at only the first 31 characters of the name.) In naming variables, consider the following:
1. DO use variable names that are descriptive;
2. DO adopt and stick with a style for naming variables;
3. DON'T start variable names with an underscore unnecessarily; and
4. DON'T name variables with all capital letters unnecessarily.
Declaring two or more variables can be in single or in different lines. An example of declaring three variables in a single line is:
int num1, num2, num3
In declaring variables in a single line, it is assumed that all variables have the same data type, hence the example above declares three variables with the data type int. The above example can also be written as:
int num1;
int num2;
int num3;
After variable declaration, next is the variable definition. Variable definition is the process of defining or assigning value to a variable. The value that should be defined to a variable must correspond to the declared data type of the variable. The variable definition uses the assignment operator = equal sign, which assigns a value in the right (right operand) to the variable in the left (left operand).
Example of variable definition:
age=18;
In the example, the value 18 is assigned to the variable named age. The variable declaration and definition can be in separate lines, but the variable declaration must always come first. An example of variable declaration and definition in separate lines is presented in figure 3.1, wherein the variable declaration is in line 4 and the variable definition is in line 5.
Figure 3.1
Variable declaration and definition can also be in a single line as presented in figure 3.2.
Figure 3.2
Variable definition that assigns numeric data to a variable is as simple as assigning the numeric value to the variable as presented in figures 3.1 and 3.2. However, if a character or string data is to be assigned to the variable, the ' ' single quotes and " " double quotes must be used. Figure 3.3 presents an example of defining a character to a variable.
Figure 3.3
As presented in figure 3.3, the character M is assigned to the variable named gender. Notice that in assigning a character to a variable, it should be enclosed with ' 'single quotes as presented in line 4. Moreover, the assignment of a string value to a variable is discussed in chapter 5.
Variable definition can take place several number of times in the program, which means that a variable can be redefined more than once and replace the previous value assigned to the variable.
Scope of variables
A scope is a region of the program and broadly speaking there are three places, where variables can be declared:
1. Local variables wherein, it is declared inside a function;
2. Global variables wherein, it is declared outside of all functions; and
3. Parameter variables wherein, it is declared in the definition of function parameters (more of this parameter variables in chapter 9 of this book) .
Meanwhile, let's deal only with what are local and global variables. Local variables are declared inside a function. They can be used only by statements that are inside that function. Local variables are not known to functions outside their own. On the other hand, global variables are declared outside of all the functions, usually on top of the main() function. The global variables will hold their value throughout the life-time of the program. A global variable can be accessed by any function. That is, a global variable is available for use throughout the entire program after its declaration. Figure 3.4 presents an example of global and local variables
Figure 3.4
In figure 3.4, lines 3 and 4 is an example of global variable declaration and line 8 is an example of local variable. The variable gNum can be used in either inside the main() function or in the other function in line 14. As presented, the global variable named gVal is defined inside the main() function in line 11 and also, the global variable gNum is defined in func_other() user-defined function in line 16. However, the local variable lNum in line 8, can only be used inside the main() function, hence it cannot be used in the func_other user-defined function in line 14. User-defined function is discussed further in Chapter 9.
Constants
A constant is also a data storage location used by a program. Unlike a regular variable, the value stored in a constant variable can't be changed during program execution. The value of a constant needs to be assigned only once, hence it cannot be redefined afterwards. C++ has two methods for defining a constant variable which are through the use of #define directive and through the use of const keyword.
The #define directive is one of C++'s preprocessor which can be placed anywhere in the source code, but they are in effect only for the portions of the source code that follow the #define directive. Most commonly, programmers group all #defines together, near the beginning of the source file and before the start of main() function.
Syntax for #define:
#define <const-varname> <value>
Note that the #define directive does not end with a ; semi-colon. In the syntax, the <const-varname> must be replaced with user-defined constant variable name, and the <value> must be replaced with the constant value.
Example of declaring constant variable using #define directive:
#define PI 3.1416
The example defines a constant variable name PI and its constant value is 3.1416 which is in fact the constant value of PI. The #define directive will determine the appropriate data type for the value assigned to it.
The const keyword is the second way to define a constant variable. const is a modifier that can be applied to any variable declaration. A variable declared to be const can't be modified during program execution and only initialized at the time of declaration.
Syntax of const:
const <data-type> <varname>=<value>;
The <data-type> must be replaced with the appropriate data type as listed in table 3.1. The <varname> is a user-defined name of the constant variable and <value> is the constant value of the constant variable.
Example of declaring a constant variable using const keyword:
const float PI=3.1416;
The example declares a constant variable named PI with a data type of float and the constant value is 3.1416.
Keywords
Keywords are predefined, reserved words used in programming that have special meaning. Keywords are part of the syntax and they cannot be used as an identifier or a user-defined name. As C++ is a case sensitive language, all keywords must be written in lowercase. Table 3.3 is a list of several keywords in C++.
Table 3.3
Along with these keywords, C++ supports other numerous keywords depending upon the compiler.
typedef declarations
Programmers can create a new name for an existing type using typedef.
Syntax of typedef:
typedef <data-type> <new-name>;
The <data-type> can be any of the data type as listed in table 3.1 and the <new-name> can is user-defined name which should also conform with the rules in naming variables.
For example, the following tells the compiler that "numeric" is another name for int:
typedef int numeric;
Now, the following declaration is perfectly legal and creates an integer variable called distance:
numeric value;
Figure 3.5 presents a sample that will display the correct size of various data types on the computer currently being use.
Figure 3.5 (source file name: ch3sample1.cpp)
This example uses endl, which inserts a new-line character after every line and << operator is being used to pass multiple values out to the screen. The program is also using sizeof() operator to get size of various data types.
Figure 3.6 present the possible output of the program but may vary from computer to computer.
Figure 3.6
Code listings of figure 3.5
#include<iostream>
using namespace std;
int main()
{
cout<<"Char Size: "<<sizeof(char)<<endl;
cout<<"Int Size: "<<sizeof(int)<<endl;
cout<<"Short int Size:"<<sizeof(short int)<<endl;
cout<<"Long int Size:"<<sizeof(long int)<<endl;
cout<<"Float Size:"<<sizeof(float)<<endl;
cout<<"Double Size:"<<sizeof(double)<<endl;
cout<<"Wchar_t Size: "<<sizeof(wchar_t)<<endl;
getchar();
return 0;
}
The fundamentals of cout is further discussed in the next chapter.
Self-assessment questions
Answer the following.
1. What is the effect of declaring a variable?
2. Can a variable be referred to before it is declared as long as long as it is declared after?
3. Can several variables be declared in the same statement?
4. What is variable definition?