Data Types in C++

Data types supported by C++ can be broadly divided into six heads as stated follows:

  1. int data-type
  2. char data-type
  3. float data-type
  4. double data-type
  5. bool (boolean) data-type
  6. enum (enumeration) data-type

1) int data-type:
int data-type represents the whole number (i.e., integer) quantities. Integers are required not to contain a decimal point or an exponent.

int data-type can also be further sub-divided into two broad categories, viz,

  1. short int and long int
  2. signed int and unsigned int

short int and long int
The qualifier short, when placed in front of the int declaration, tells the C++ system that the particular variable being declared will be used to store fairly small integer values.
On the other hand, the qualifier long, when placed in front of the int declaration, tells the C++ system that the particular variable being declared will be used to store fairly large integer values.

signed int and unsigned int
In case of signed int (or even in case of ordinary int, short int, long int as by default every int is signed int), the leftmost bit (of computer memory) is reserved for the sign. However in case of unsigned int, there is no such kind of reservation and so all the bits are used to represent the numeric value. Thus, an unsigned int can be approximately twice as large as an ordinary int.

2) char data-type:
char data-type is used to represent individual characters. Hence, the char type generally requires only one byte of memory. Just like signed int and unsigned int, C++ language permits signed char and unsigned char.

3) float data-type:
float data-type (also called as floating point) represents values containing decimal places. A floating point value is distinguished by the presence of a decimal point. It is permissible to omit digits before the decimal point, or digits after the decimal point, but obviously not permissible to omit both.
The values 3., 125.8 and -.001 are all valid examples of floating point values.
Floating point values can also be expressed in so called scientific notation. The value 17.e4 is a floating point value expressed in this notation, and represents the value 1.7 x 104. The value before the letter e is known as the mantissa while the value that follows the letter e is called the exponent.

4) double data-type:
double data-type is very similar to float data-type. It is used whenever the accuracy provided by a float variable is not sufficient. Variables declared to be of type double can store roughly twice as many significant digits as can a variable of type float can store.

5) bool (boolean) data-type:
This is a new addition to C++. The data type bool gets its name from George Boole, a 19th century English mathematician who invented the concept of using logical operators with true or false values. These values are often called boolean values.

This data type can take only two values true or false. It is commonly used to hold the results of comparisons.

For example:

bool a, b;
int x = 10, y = 20, z = 30;
a = x < y;
b = y>= z;

Here, a gets a value true. whereas, b gets a value false.

By definition, true has a value 1 (when converted to an integer) and false has a value 0 (when converted to an integer).

Programming example along with its output:

int main()
{
bool flag = false;
cout << "flag = " << flag <<endl;

flag = true;
cout << "flag = " << flag << endl;
}

Output:
flag = 0
flag = 1

6) enum (enumeration) data-type:
An enumeration data type is an integral type that is defined by the user and has the syntax as:

enum typename {enumerator-list};

where enum is a C++ keyword,
typename stands for an identifier that names the type being defined, and
enumerator-list stands for a list of names for integer constants.

For example, the following defines the enumeration type semester, specifying the three possible values that a variable of that type can have:

enum semester {FALL, SPRING, SUMMER};

We can then declare the variables of this above type as:

semester s1, s2;

and we can use those variables and those type values as we would with predefined types as:

s1 = SPRING;
s2 = FALL;
if (s1 == s2)
{
cout << "Same Semester." <<endl;
}

You may also like...

3 Responses

  1. Eric says:

    I blog quite often and I truly thank you for your information. Your article has really peaked my interest.
    I’m going to take a note of your blog and keep checking for new
    information about once a week. I subscribed to your Feed
    as well.

  2. Jake says:

    In #4 above (describing the ‘double’ data type), there’s a typo that can cause confusion to the reader: “Variables declared to be of type float can store roughly twice as many significant digits as can a variable of type float can store.”

    It should read: “Variables declared to be of type __double__ can store roughly twice as many significant digits as can a variable of type float can store.”

Leave a Reply

Your email address will not be published. Required fields are marked *