Data Types in C++

Filed Under: C++

Data types help the associated variables understand the type of data they can store into them.

C++ Data Types
C++ Data Types

User-Defined Data Types in C++

C++ Language provides us with User-Defined Data types that are the data types created by the user/programmer.

Following are the types of user-defined types in C++:

  • Structure
  • Union
  • Enumeration
  • Class

1. Structure

Structure type, groups elements of different data types under a custom data type (structure) and is represented by a single structure name.

Syntax:

struct Structure_Name 
{
    Datatype data_member1;
    Datatype data_member2;
    .
    .
    Datatype data_memberN;
};

Example:

struct Student_info
{ 
char name[100]; 
char address[100]; 
char division[50]; 
int roll_num; 
};

2. Union

Union types serve the same functionality as that of the Structures.

The only difference is that in Unions, all the data members share the same space of memory which is equivalent to the size of the largest variable, during the execution of the program.

Syntax:

Union Union_Name 
{
    Datatype data_member1;
    Datatype data_member2;
    .
    .
    Datatype data_memberN;
};

Example:

union Student_info
{ 
char name[100]; 
char address[100]; 
char division[50]; 
int roll_num; 
};

In the above piece of code, name and address are largest among all the data members declared because we specified their size to 100.

So, the compiler will allocate the size of the largest variable i.e. name or address, to the memory storage to accommodate them and all the variables will share the same memory space (100) and address.

3. Enumeration

Enumeration types help increase the readability of the code. It assigns names to the integers from the program.

These types are indexed from zero resembling the indexing fashion of arrays.

Syntax:

enum enumeration_type_name{value1, value2,..valueN};

Example:

#include <iostream> 
using namespace std; 
  
enum Days { Mon, 
            Tue, 
            Wed, 
            }; 
  
int main() 
{ 

    for (int i = Mon; i <= Wed; i++) 
        cout << i << " "; 
  
    return 0; 
} 

Output:

0 1 2 

Derived Data Types in C++

The Derived types are derived or formed from the built-in/primitive data types.

Following are the types of derived data-types:

  • Arrays
  • Function
  • Pointers

1. Array

An array is a linear data structure that stores the elements in contiguous memory locations in a linear/sequential manner. The elements are indexed from zero.

Syntax:

Data_type array_Name[size];

Example:

#include <iostream> 
using namespace std;
int main() 
{ 

	// Array Derived Type 
	int arr[4]; 
	int sum=0;
    cout<<"Enter the elements..\n";
    for(int i =0; i<4; i++)
    {
        cin>>arr[i];
    }

   for(int i=0; i<4; i++)
   {
    sum+=arr[i];   
   }

   cout<<"Addition of elements of array:\n ";
   cout<<sum;
	return 0; 
} 

Output:

Enter the elements..
10
20
30
40
Addition of elements of array:
 100

2. Function

Functions are a block of statements that particularly perform a set of tasks under it. They make the code more efficient and readable.

Syntax:

Data_type function_Name(Arguments)

Example:


#include <iostream> 
using namespace std; 


int sum(int a, int b) // function definition
{   int sum=0;
	sum = a+b;
	return sum;
}	

 
int main() 
{ 
	int a = 20, b = 40; 
        int result = sum(a, b); // Calling the function
        cout <<"Result of addition:\n"<< result; 
	return 0; 
} 

In the above snippet of code, we have performed an addition of two numbers using a function.

Output:

Result of addition:
60

3. Pointers

Pointer types represent the address of the data members. They hold the address of another data member to which they point.

Syntax:

data_type *variable;

Example:

#include <iostream> 
using namespace std; 


int main() 
{   int val = 10;
	int* p;
	p = &val;
	cout<<"p = "<<p<<"\n"; //address of val
    cout<<"val = "<<val<<"\n";	
    cout<<"*p = "<<*p<<"\n";
} 

Output:

p = 0x7ffd328d4a34
val = 10
*p = 10

Primitive Data Types in C++

Primitive data types are the built-in data types directly available for the user to set out the operations.

1. Integer (int)

Integer data type holds 2 bytes of memory. It ranges from 2147483648 to 2147483647.

Syntax:

int variable = value;

2. Character (char)

Character data types are used to store character values. Character types hold 1 byte of memory space and range from 128 to 127 or 0 to 255.

Syntax:

char variable = 'val';

3. Float (float)

Float data types are used to store single-precision data values i.e. decimal values. It holds 4 bytes of memory space.

Syntax:

float variable = val;

4. Double (double)

Double data types are used to store double-precision floating-point data values. It holds 8 bytes of memory space.

Syntax:

double variable = val;

5. Boolean (bool)

Boolean types store and represent logical values. They represent the result in the form of True or False.

bool variable = true/false;

6. Void

Void types represent entities without any value. They are used as a data type for functions that do not return any value.


Conclusion

Thus, in this article, we have understood the data types in the C++ language.


References

Leave a Reply

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

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages