C++ Data Types


In both C and C++, different variables are used to represents various information. These variables are stored in the memory. Hence, the type of these variables represents how much memory requires to store these variables. Whenever a variable is declared, based on the data type of the variable, the compiler allocates memory to that variable.

The C++ data types can be categorized as follows-

C++ Data Types

Built In Data Type in C++

The built in data type is predefined and can be directly used by the programmer to declare variables. These data types are also called as Fundamental Data Types or Primitive Data Types. The built in data types are categorized in 4 types. These are-

Integer Type in C++

The integer data type has two types. These are-

  • int : It is used to store integer type data. For example : int a = 2; Here, the integer variable a is initialized with the integer value 2.
  • Char : It is used to store character type data. For example: char name = ‘a’; Here the character variable name is initialized with the character a.

Floating Type in C++

This data type is used to store floating point numbers. It is of 2 types-

  • float: it is used to store single precision floating point values. For example: float a = 10.2; Here, the floating variable a is initialized with a single precision floating value 10.2
  • double: it is used to store double precision floating point number. For example : double a = 3.145679; Here, the double variable a is initialized with a double precision floating point 3.145679.

void type in C++

void is the another built in data type which is mainly used for 2 reasons-

  • To specify the return type of a function, when the function does not return any value,
  • To specify an empty list of arguments.

bool type in C++

bool data type is used to store the value of Boolean type variables. It stores the values in either true or false form or as 0 or 1.

The built in data types are modified by length of the data by using several modifiers. These modifiers are-

  • signed: the signed data type is the default type and it includes both positive and negative data type.
  • unsigned: It is without any sign and hence always positive in nature.
  • short: The short modifier is used to modify the minimum value that a data type can hold.
  • long: The long modifier is used to modify the maximum value that a data type can hold.

Here is the list of all the built in data type along with the modifiers and their bit width-

Type Byte width
int 4 bytes
signed int 4 bytes
unsigned int 4 bytes
short int 2 bytes
long int 4 bytes
signed short int Range
unsigned short int Range
signed long int 4 bytes
unsigned long int 4 bytes
char 1 byte
signed char 1 byte
unsigned char 1 byte
float 4 bytes
double 8 bytes
long double 8 bytes
bool 1 byte
wchar_t 2 or 4 bytes

 



User Defined Data in C++

In the real world problem, the built in data types are not sufficient to handle all the variables. Hence, some user defined data types are used to group the related data items together. The user defined data types are:

Structure in C++

C++ permits the user to group the dissimilar data types together. Structure is used to group dissimilar data types together. It is defined with the keyword struct. It has the size equal to the total size of individual member elements. It has the syntax as follows-

struct structure_name
{
//declaration of member elements;
}structure_variables;
For example:
struct employee
{
int id;
char name[20];
}e1,e2;

 

Here, the keyword struct declares a new data type employee which is holding 2 different data types. These data fields are known as structure_member or structure_elements. The employee is the structure_name or structure_tag. The e1, e2 are the structure_variables which are used to call the member variables.

Union in C++

The union data type is similar to the structure data type, but the only difference is that it can manipulate one member at a time. It is declared with the keyword union. It has the size equal to the size of the largest member elements. It has the syntax as follows-

union union_name
{
//declaring member variables;
}union_variables;
For example:
union student
{
int age;
long roll_no;
} s1;

 

Here, the keyword union declares a new data type holding 2 different data types and with the size equal to the largest member variable. The student is the union_name and s1 is the union_variable.

Class in C++

In C++, like structure and union, another used defined data type class is used to hold different dissimilar data types together. It is defined with the keyword class. The class variables are known as object which is the main aspect of object-oriented programming. It has the syntax as follows-

class class_name
{
variable_declarations;
function_declarations;
public:
variable_declarations;
function_declarations;
} objects;

 

For example:

class student
{
int roll;
public:
void details();
float result();
} s1, s2;

 

Here, the user defined data type student is defined with the keyword class. It has the member variables and member functions with different data types. s1, s2 are the objects of the type student. The members have 3 access specifiers- private, public and protected. If the access specifier is not mentioned, then by default it is always private.

Enumerated Data Type in C++

the enumerated data type provides an way of attaching the names with numbers by declaring a new type name and a set of values starting from 0 to more as the value of the identifier. The value of the identifier started from 0 and incremented by 1. It is defined with the keyword enum. It has the syntax as follows-

enum enum_name (list_of_names)variable;

 

For example:

Enum month(jan, feb, mar, apr)m;

 

Here the enumeration of months is defined with the variable m. Here, jan = 0, feb = 1, mar = 2 and apr = 3.



Derived Data Types in C++

The data types derived from the built in data type are known as derived data type. It is categorized into 4 types. These are:

Array in C++

An array is used to store a set of similar type of data. It has a fixed size i.e. the size of the array cannot be changed at the run time. All the elements of the array is stored in the continuous memory blocks. Hence, it is termed as static data structure. It has the syntax as follows-

data_type array_name[index_size];

 

Here, data_type refers to the type of the data to be stored in the array. Array_name is the name of the array and index_size refers to how many elements can be stored in the array. For example:

int roll[20];

 

Here, in the array roll, 20 integers value can be stored.

Function in C++

Function is the collection of variables and statements to carried out a specific task. In both C and C++, every program contain one or more function. Each program of C and C++ must contain the main() function. It has the syntax as follows-

Return_type function_name(argument_list)
{
//function_body;
}

 

Return_type refers to the value return by the function. Function_name refers to the name of the function. Argument list or parameters refers to the type, order and number of parameters of a function. The function may not have any parameters, then void type is used as parameter. The function_body contains all the variables and statements to carry out the specific task. For example:

int add(int a,int b)
{
int sum;
sum = a + b;
return sum;
}

 

Here, a function add is defined with the return type int and have two parameters a and b of int type.

Pointers: Pointer is used to store the memory address of another variable. That is it points to the address of the variable rather than the value of the variable. By using pointers, we can use the memory locations dynamically which is not possible in array. Hence, the memory is allocated at the run time and the memory is not wasted.  It has the syntax as-

Data_type *pointer_name;

 

Here, data_type refers to the type of the pointer and pointer_name is the identifier. For example:

int *num;

 

Here, the pointer num is declared with the type integer.

num = &no;

 

Here, the variable num is assigned with the address of the variable no.

*num = no;

 

Here, the variable num holds the value of no.

Reference: In C++, the reference is the alternative name for a variable. The variable can be called with the reference also. It uses the same memory space as the variable. It has the syntax as-

Data_type &reference_name = variable_name;

 

For example:

int num = 20;
int &no = num;

 

Here, the reference variable no holds the variable num and its value.

Close Menu