Basic Data Types User-defined data types,

Basic Data Types

The basic data types are integer-based and floating-point based. C++ language supports both signed and unsigned literals.

The memory size of basic data types may change according to 32 or 64 bit operating system.

Let's see the basic data types. It size is given according to 32 bit OS.

Data Types Memory Size Range
char 1 byte -128 to 127
signed char 1 byte -128 to 127
unsigned char 1 byte 0 to 127
short 2 byte -32,768 to 32,767
signed short 2 byte -32,768 to 32,767
unsigned short 2 byte 0 to 32,767
int 2 byte -32,768 to 32,767
signed int 2 byte -32,768 to 32,767
unsigned int 2 byte 0 to 32,767
short int 2 byte -32,768 to 32,767
signed short int 2 byte -32,768 to 32,767
unsigned short int 2 byte 0 to 32,767
long int 4 byte
signed long int 4 byte
unsigned long int 4 byte
float 4 byte
double 8 byte
long double 10 byte

 

Data  types in C++:

All variables (Links to an external site.) use data-type during declaration to restrict the type of data to be stored. Therefore, we can say that data types are used to tell the variables the type of data it can store. Whenever a variable is defined in C++, the compiler allocates some memory for that variable based on the data-type with which it is declared. Every data type requires a different amount of memory.
 

Data types in C++ is mainly divided into three types: 
 

  1. Primitive Data Types: These data types are built-in or predefined data types and can be used directly by the user to declare variables. example: int, char , float, bool etc. Primitive data types available in C++ are: 
    • Integer
    • Character
    • Boolean
    • Floating Point
    • Double Floating Point
    • Valueless or Void
    • Wide Character
  2. Derived Data Types: (Links to an external site.) The data-types that are derived from the primitive or built-in datatypes are referred to as Derived Data Types. These can be of four types namely: 
    • Function
    • Array
    • Pointer
    • Reference
  3. Abstract or User-Defined Data Types (Links to an external site.): These data types are defined by user itself. Like, defining a class in C++ or a structure. C++ provides the following user-defined datatypes: 
    • Class
    • Structure
    • Union
    • Enumeration
    • Typedef defined DataType

This article discusses primitive data types available in C++. 
 

  • Integer: Keyword used for integer data types is int. Integers typically requires 4 bytes of memory space and ranges from -2147483648 to 2147483647. 
     
  • Character: Character data type is used for storing characters. Keyword used for character data type is char. Characters typically requires 1 byte of memory space and ranges from -128 to 127 or 0 to 255. 
     
  • Boolean: Boolean data type is used for storing boolean or logical values. A boolean variable can store either true or false. Keyword used for boolean data type is bool
     
  • Floating Point: Floating Point data type is used for storing single precision floating point values or decimal values. Keyword used for floating point data type is float. Float variables typically requires 4 byte of memory space. 
     
  • Double Floating Point: Double Floating Point data type is used for storing double precision floating point values or decimal values. Keyword used for double floating point data type is double. Double variables typically requires 8 byte of memory space. 
     
  • void: Void means without any value. void datatype represents a valueless entity. Void data type is used for those function which does not returns a value. 
     
  • Wide Character (Links to an external site.): Wide character data type is also a character data type but this data type has size greater than the normal 8-bit datatype. Represented by wchar_t. It is generally 2 or 4 bytes long. 
     

 

 

 

Datatype Modifiers

As the name implies, datatype modifiers are used with the built-in data types to modify the length of data that a particular data type can hold. 
 

Data type modifiers available in C++ are: 
 

  • Signed
  • Unsigned
  • Short
  • Long

Below table summarizes the modified size and range of built-in datatypes when combined with the type modifiers:
 

Data Type Size (in bytes) Range
short int 2 -32,768 to 32,767
unsigned short int 2 0 to 65,535
unsigned int 4 0 to 4,294,967,295
int 4 -2,147,483,648 to 2,147,483,647
long int 4 -2,147,483,648 to 2,147,483,647
unsigned long int 8 0 to 4,294,967,295
long long int 8 -(2^63) to (2^63)-1
unsigned long long int 8 0 to 18,446,744,073,709,551,615
signed char 1 -128 to 127
unsigned char 1 0 to 255
float 4  
double 8  
long double 12  
wchar_t 2 or 4 1 wide character

Note : Above values may vary from compiler to compiler. In the above example, we have considered GCC 32 bit.
We can display the size of all the data types by using the sizeof() operator and passing the keyword of the datatype as argument to this function as shown below: 
 

// C++ program to sizes of data types
#include<iostream>
using namespace std;
 
int main()
{
    cout << "Size of char : " << sizeof(char)
      << " byte" << endl;
    cout << "Size of int : " << sizeof(int)
      << " bytes" << endl;
    cout << "Size of short int : " << sizeof(short int)
      << " bytes" << endl;
    cout << "Size of long int : " << sizeof(long int)
       << " bytes" << endl;
    cout << "Size of signed long int : " << sizeof(signed long int)
       << " bytes" << endl;
    cout << "Size of unsigned long int : " << sizeof(unsigned long int)
       << " bytes" << endl;
    cout << "Size of float : " << sizeof(float)
       << " bytes" <<endl;
    cout << "Size of double : " << sizeof(double)
       << " bytes" << endl;
    cout << "Size of wchar_t : " << sizeof(wchar_t)
       << " bytes" <<endl;
     
    return 0;
}

Output: 
 

Size of char : 1 byte
Size of int : 4 bytes
Size of short int : 2 bytes
Size of long int : 8 bytes
Size of signed long int : 8 bytes
Size of unsigned long int : 8 bytes
Size of float : 4 bytes
Size of double : 8 bytes
Size of wchar_t : 4 bytes




Introduction to User Defined Data Types in C++

User Defined Data type in c++ is a type by which the data can be represented. The type of data will inform the interpreter how the programmer will use the data. A data type can be pre-defined or user-defined. Examples of pre-defined data types are char, int, float, etc. We will discuss user-defined data types in detail.

As the programming languages allow the (Links to an external site.) user to create their own data types according to their needs. Hence, the data types that are defined by the user are known as user-defined data types. For example; arrays, class, structure, union, Enumeration, pointer, etc. These data types hold more complexity than pre-defined data types.

Types of User-Defined Data in C++

Here are the types mentioned below:

Types of User-Defined Data in C++

1. Structure

A structure is defined as a collection of various types of related information under one name. The declaration of structure forms a template and the variables of structures are known as members. All the members of the structure are generally related. The keyword used for the structure is “struct”.

For example; a structure for student identity having ‘name’, ‘class’, ‘roll_number’, ‘address’ as a member can be created as follows:

struct stud_id
{
char name[20];
int class;
int roll_number;
char address[30];
};

This is called the declaration of the structure and it is terminated by a semicolon (;). The memory is not allocated while the structure declaration is delegated when specifying the same. The structure definition creates structure variables and allocates storage space for them. Structures variables can be defined as follows:

stud_id I1, I2;

Where I1, I2 are the two variables of stud_id. After defining the structure, its members can be accessed using the dot operator as follows:

I1.roll_number will access roll number of I1

I2.class will access class of I2

Example:

struct stud_id
{
int class, roll_number;
};
int main()
{
struct stud_id entries[10];   // Create an array of structures
entries[0].class = 4;           // Access array members
entries[0].roll_number = 20;
cout <<entries[0].class << ", " << entries[0].roll_number;
return 0;
}

2. Array

An Array is defined as a collection of homogeneous data. It should be defined before using it for the storage of information. The array can be defined as follows:

<datatype> <array_name><[size of array]>
int marks[10]

The above statement defined an integer type array named marks that can store marks of 10 students. After the array is created, one can access any element of an array by writing the name an array followed by its index. For example; to access 5th element from marks, the syntax is as follows:

marks[5]

It will give the marks stored at the 5th location of an array. An array can be one-dimensional, two-dimensional or multi-dimensional depending upon the specification of elements.

Example:

int main()
{
int marks[10];
marks[0] = 5;
marks[2] = -10;
cout<<marks[0], marks[2]);
return 0;
}

3. Union

Just like structures, the union also contain members of different data types. The main difference between the two is that union saves memory as members of a union share the same storage area whereas members of the structure are assigned their own unique storage area. Unions are declared with keyword “union” as follows:

union employee
{
int id;
double salary;
char name[20];
}

The variable of the union can be defined as:

union employee E;

To access the members of the union, the dot operator can be used as follows:

E.salary;

4. Class

A class is an important feature of object-oriented programming language just like C++. A class is defined as a group of objects with the same operations and attributes. It is declared using a keyword “class”. The syntax is as follows:

class <classname>
{
private:
Data_members;
Member_functions;
public:
Data_members;
Member_functions;
};

In this, the names of data members should be different from member functions. There are two access specifiers for classes that define the scope of the members of a class. These are private and public. The member specified as private can be only accessed by the member functions of that particular class only. However, the members defined as the public can be accessed from within and from outside the class also. The members with no specifier are private by default. The objects belonging to a class are called instances of the class. The syntax for creating an object of a class is as follows:

<classname> <objectname>

Example:

class kids
{
public:                //Access specifier
char name[10];   //Data members
int age;
void print()         //Member function
{
cout<<”name is:”<< name;
}
}
Int main
{
Kids k;                    //object of class kid is created as k
k.name=”Eash”;
k.print();
return 0;
}

5. Enumeration

Enumeration is specified by using a keyword “enum”. It is defined as a set of named integer constants that specify all the possible values a variable of that type can have. For example, enumeration of the week can have names of all the seven days of the week as shown below:

Example:

enum week_days{sun, mon, tues, wed, thur, fri, sat};
int main()
{
enum week_days d;
d = mon;
cout << d;
return 0;
}

6. Pointer

A Pointer is that kind of user-defined data type that creates variables for holding the memory address of other variables. If one variable carries the address of another variable, the first variable is said to be the pointer of another. The syntax for the same is:

type *ptr_name;

Here type is any data type of the pointer and ptr_name is the pointer’s name.

User Defined Data Types in C++(pointer)

Example:

void main()
{
int a = 10;
int *p;   // pointer variable is declared
p = &a;  // data type of pointer ‘p’ and variable ‘a’ should be same
cout<<"Value at p = ",<<p); // the address of a variable is assigned to a pointer
cout<<"Value at variable a = “,<<a);
cout<<"Value at *p = ",<< *p);
}

7. Typedef

Using the keyword “typedef”, you can define new data type names to the existing ones. Its syntax is:

typedef <type> <newname>;
typedef float balance;

Where a new name is created for float i.e. using balance, we can declare any variable of float type.

The use of a typedef can make the code easy to read and also easy to port to a new machine.

Example:

typedef  int score;
int main()
{
score s1, s2;
s1 = 80;
cout << " " << b1;
return 0;
}

Conclusion

C++ supports different kinds of user-defined data types as discussed above. There many other such data types such as functions, reference, etc. Their use makes programming much easier and they also help us to club different types of data in a single variable.