C++ Data Types

  • Data types determine the type of value stored in any variable or constant. In simple words, what kind of value is hold by the variable?
  • Size of value stored in the variable, constant or array are also determined the data type.
  • In C++ Data Types can be categorized into following parts:
    • Basic or primitive data types
    • Derived data types
    • Enumerated data types
data-type Basic or primitive data type:
  • The data types which are predefined in the C++ language and these are most commonly used to define the type of value stored in any variable, constant or array.
  • The primitive data types provided by the C language are as follows:
  • Integer data type: used to store integer values. C++ includes various integer type data types like short (signed), int (signed) , unsigned short, unsigned int, long(signed), unsigned long. Here using signed, we can store both negative and positive value in the variable but using unsigned, we can use only positive values.
  • Character data type:used to store a single character. C++ language includes two types of character data type char (signed char) and unsigned char.
  • Floating point data type: Floating point data type store decimal values. C++ includes various floating point data type float, double, long double etc.
  • Boolean type: The boolean type can store only two types of value that are true or false. Boolean data type is represented by bool keyword.
  • The entire primitive data types supported by C++ are explained below in the table:
data-type Derived or user-defined data type
  • The data types which are derived from the basic or primitive data types are known as Derived data types like array, structure, union, class, Pointer, function etc.
  • We will study each of these derived data type in further section.
Enumerated data type
  • Enumerated data types are user defined data types.
  • Enumerated data types are declared using enum keyword.
  • It arranges a list of items in a sequence and associates a symbolic number to each element. Basically it is used when we have fixed item to choose.
  • Syntax of Enumerated data type in C++:
  • enum idenitifer{list of items);
  • Here is an example of enum containing the list of seven days of a week:
  • enum week { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday};
  • In the above example Sunday, Monday, Tuesday and so on are the list of enumerated item. Their values are constant unsigned integers and start from 0 by default. So Sunday refers to 0, Monday refers to 1 and so on.
  • Here is a simple program of enum:

Example

#include<iostream.h>

#include<conio.h>

void main()

{

enum week { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday};

cout<<"\nSunday="<<Sunday;

cout<<"\nMonday="<<Monday;

cout<<"\nTuesday="<<Tuesday;

cout<<"\nWednesday="<<Wednesday;

cout<<"\nThursday="<<Thursday;

cout<<"\nFriday="<<Friday;

cout<<"\nSaturday="<<Saturday;

}

Output

Sunday=0

Monday=1

Tuesday=2

Wednesday=3

Thursday=4

Friday=5

Saturday=6