Variables, and Data Types



Another way to look at it is, a variable is a bucket which can be used to hold something (usually a numeric value).


They are called variables because we can change the data value stored there, thus the value can vary. We use an identifier to reference a variable in a program. A variable is created in code by giving its data type and its identifier in the format:

     dataType identifier;

Data Types

There are six basic data types in the C programming language. They are sometimes referred to as built-in or atomic data types.
   

Variables of this type are used to hold single characters.
That is, any of the 256 ASCII character set characters.


This is one of several integer data types. This one is
rarely used now since memory is cheap and it has such
a limited range of values.

This is one of several integer data types. This one is
also the one that is used most. In the original version
of C the int and short were the same size. Now the int
and long are the same size.


This is one of several integer data types. In the original
version of C the long was used when you needed greater value
range. Because it is the same size as an int now it is not
used much.


This is one of several real or decimal data types. The
float, like the short is not used much now.
This is one of several real or decimal data types. It is the
one that is used most now since it gives greater
precision and a wider range of values.

With the development of the C++ programming language three more built-in or atomic data types were added.
   

This is newest of the integer data types. Notice that its
size is twice that of the int and long and it has a
significantly wider value range.


This is newest of the real or decimal data types. Notice that its size varies from
one system to another. On a PC running Windows it is the same size as a double.


This data type is now used extensively whenever you need to
set a boolean flag to indicate a true or false state
for anything.


Code Examples

Below is a simple program which creates a variable of each of the six basic data types, plus bool. As each variable is created it is initialized with an appropriate value. Then all of the values are printed using the cout operator.

	#include 
	using namespace std;

	int main()
	{
		char c = 'z';
		bool b = true;
		short s = 32;
		int i = 12345;
		long l = 123456789;
		float f = 2.14159F;
		double d = 123.456789;

		// Print the values
		cout << "Variable c = " << c;
		cout << "Variable b = " << b;
		cout << "Variable s = " << s;
		cout << "Variable i = " << i;
		cout << "Variable l = " << l;
		cout << "Variable f = " << f;
		cout << "Variable d = " << d;

		return 0;
	}


Constants

This may sound strange, but you can also create a special type of variable whose value you cannot change. These special variables are call, appropriately enough, Constants.




A constant is created by prepending a regular variable declaration with the keyword const. You must also initialize the constant identifier at the time it is created as shown in the examples below. By convention, most programmers spell the name of the constant identifier using all capital letters so it is easily recognized when used in code.



Below are some examples of constant declarations. You can create constants of any of the basic data types in C++ as well as the string object.