Datatypes in Cpp

Datatypes in Cpp

What is Data types ?

Data types are a set of values and are determined to act on those values. The type of a variable determines how much space it occupies in storage. 
In short data types are used to specify the type of data. The value of a variable can be changed any time.

In this tutorial you will know about :

Why do we need Data Types ?

1. To identify the type of a variable when it is declared.
2. To Identify the type of a parameter expected by a function.
3. To Identify the type of the return value of a function.

How to declare Data Types ?

The taken variable names are needed to be assigned with a data type. This is how the data types are used along with variables :

Example :
int eruditors;
float eru;
char team, eruditors;

Divisions of Data Types :

C++ Programming Language has 4 types of data types.
1. Primary/Basic Data Types : int, float, char, double.
2. Derived Data Types : array, pointer, function.
3. Abstract or User-Defined Data Types : structure, class, union
4. Enumeration Data Types : enum.

1. Primary/Basic Data Types : 

This primitive data types are divided into 4 parts. They are :

A. Integer Data Types : 

Integer is defined as a number that can have both zero, positive and negative values but has no fractional component.

Example :
0, -47, 91

Key Points about Integer Data Types :
~ Integer data is represented by its own datatype known as int.
~ It requires 2 bytes of memory for 16bit compilers and 4 bytes of memory for both 32 & 64 bit compilers.
~ The Range of Integer Data Type : -32,768 to 32,767

B. Float and Double Data Types :

Numbers which have a fractional component is known floating point numbers or float. Both float & double are used to hold real numbers.

Example :
117.56, 101.211

Key Points about Float Data Types :
~ Float data is represented by its own data type known as float.
~ It requires 4 bytes of memory in almost all compilers.

Key Points about Double Data Types :
~ Double data is represented by its own data type known as double.
~ It requires 8 bytes of memory in almost all compilers.

Difference between Integer and Float Data Types :
1. Integers consumes significantly less space than Floating point numbers.
2. Floating point numbers can represent numbers accurately.

C. Character Data Types :

It is used to store characters and letters.

How to use character data types :
Syntax :
char team = 'j';

Key Points about Character Data Types :
~ Character data is represented by its own data type known as char.
~ It requires 1 byte of memory.
~ The character is also integer type because underneath C Programming Language stores integer numbers instead of characters. In order to represent characters, the computer has to map each integer with a corresponding character using a numerical code.
For example, the integer number 97 represents the 'a' character in lower case.

2. Derived Data Types :

Data types which are derived from fundamental data types are known as derived data types. This derived data types are divided into 5 parts. Those are :

A. Array :

An array is a collection of similar data items stored at contiguous memory locations and the array elements can be accessed randomly using index of that array. In simple word an array is a variable that can store multiple values of same data type.

B. Pointer :

A pointer is a special variable whose value is the address of another variable. In simple word, Pointer is also kind of a variable that is used to store address of variables or a memory location.

C. Function :

A function is a block of code that takes input, performs some specific tasks and produces output.

3. Abstract or User-Defined Data Types :

Data types which are defined by the user itself are known as User-Defined data types. These types include :

A. Class :

Class is like a blueprint of an object. Class holds its own data members and member functions, which can be accessed and used by creating an instance of that class.

B. Structure :

Structure can store logically related information of different data types. Structure creates a data type that can be used to group items of possibly different types into a single type.

C. Union :

Union is a collection of variables of different data types in the same memory location. Simply it can e said that if two variable named anik and deb share the same location and then we change the value of anik, we can see the changes being reflected in deb.

4.Enumeration Data Types :

Enumeration is a user defined data type which is used to assign names to integral constants. It makes a program more readable and maintained.

How to use enumerated data types :
Syntax :
enum tagname
   identifier1, identifier2,…….,identifier n

Example :
using namespace std;
enum week{Sun, Mon, Tue, Wed, Thur, Fri, Sat}; 
int main()
    enum week day;
    day = Fri;
    return 0;

//here we declared "day" as the variable and "Fri" is allocated to "day".

Output :
//The value of "Fri" is allocated to "day" which is 5.
//So 5 is printed as output.

Key Points about Enumerated Data Types :
~ Enumerated data is represented by its own datatype known as enum.
~ By default, the compiler assigns values starting from 0. In the upper program "Mon" gets value 0, "Tue" 1 and so on. The default values of enum elements are changeable during declaration.
~ Values can be assigned to some name in any order. All unassigned names get value after the previous value's name +1

Example :
using namespace std;
enum week{Sun=2, Mon, Tue, Wed=8, Thur, Fri=11, Sat};
int main()
cout <<Sun <<endl <<Mon <<endl <<Tue <<endl <<Wed <<endl <<Thur <<endl <<Fri <<endl <<Sat;
return 0;

Output :
~ The assigned values to enum names must be some integral constant. The range must be from minimum possible integer value to maximum possible integer value.
~ All the enum constants must be unique in their scope.