Data Types in C++.

Data Types are one of the most essential topics of any programming language that you should understand whenever you are learning any new programming language. The data type can be defined as which type of value a variable can store and what kind of mathematical, logical, and relational operations are allowed on those values.

In C++ programming, data types can be classified into the following groups:
  • Primitive/Built-in Datatypes.
  • Derived Datatypes.
  • User-Defined Datatypes.
data type in C++
There are several data types available in the C++ programming language.

Primitive Datatypes in C++.

Primitive data types are built-in datatypes that are already available to use in C++ programming languages. There are 7 primitive data types available in C++ programming and these are integers, float, double, character, boolean, void, and wide character. 

1. Integer
An integer data type is represented by the int keyword in C++  and each integer value takes up to 4 bytes of memory. The range of an integer-type variable is -2147483648 to 2147483647, this is the minimum and maximum value that you can store in an integer-type variable. Example: int num = 3450;

2. Floating point
A floating point data type is represented by the float keyword in C++ and is used to store decimals and exponential values. Each floating point value requires 4 bytes of memory. Example: float value = 45.15;

3. Double
Like floating data, Double data types are also used to store decimals and exponential values but double requires 8 bytes of memory because the precision of double is greater than float. Example: double value = 45.1502;

4. Boolean
Boolean datatype is represented by the bool keyword in C++ and can store only two types of values 'true' or 'false'. Example bool check = true;

5. Character
Character datatype is represented by the char keyword in C++ and requires 1 byte of memory. Characters are enclosed inside single quotes(' ') in C++. Example: char ch = 'a';

6. void
The term void means "no value" or "nothing", it is the keyword that is used to indicate the absence of data. There is no variable of type void, it is only used with those function which does not return any value.

7. Wide Character
Wide character (wchar_t) is the same as character data type but unlike char, it takes 2 or more bytes and is used to represent the character that requires more memory. Example: wchar_t w = L'A' 

Derived data types in C++

The data types that are derived from primitive or built-in data types are known as Derived Datatype. 

1. Function
A function is a block of code with a set of instructions that is used to perform a specific task. We create functions to avoid writing the same lines of code again and again. We can call the function anywhere inside the code whenever we want to perform that same task. 

Example: This function is used to perform the addition of two numbers and return a result of type integer.  
int sum(int a, int b){
  return (a + b);
}

2. Array
An array is a Linear Data Structure that can store elements of similar data types in a contiguous memory location. The main use of an Array is to store many values in just one variable. 
syntax: data_type ArrayName[size_of_array];

Example: int arr[5] = {2, 4, 6, 7, 1};

3. Pointers
A pointer is a variable that is used to hold the address of another variable. It stores the address of the variable having the same data type (integer pointer can hold the address of integer type variable). Example: int *ptr;

4. Reference
Reference is like an alternative name for another existing variable. It is represented by putting the '&' symbol in front of any variable. Example: int &ref = num;


User-Defined data types in C++

User-defined datatypes are created by users based on their requirements and they are created by using existing built-in data types.

1. Class
Class is a user-defined data type that has its own data members and member functions. It is a very important concept of object-oriented programming in C++. 
//Example of C++ class program
#include<iostream>
using namespace std;

class Addition{
  public:
  int result; //data member

  void add(int x, int y){ //member function
    result = x + y;
    cout<<"Result: "<<result;
  }
};

int main(){
  Addition obj;
  obj.add(5, 10);
  return 0;
}
Output:
Result: 15

2. Structure
A structure is a user-defined data type that is used to combine different data types into a single type. It is represented using the struct keyword in C++.
//Example of C++ structure program
#include<iostream>
using namespace std;

struct student{
  int rollNo;
  string name;
  char grade;
};

int main(){
  student std;
  std.rollNo = 10;
  std.name = "John";
  std.grade = 'A';

  cout<<std.name<<endl;
  cout<<std.rollNo<<endl;
  cout<<std.grade<<endl;

  return 0;
}
Output:
John
10
A

3. Enumeration
Enumeration is a user-defined data type denoted by an enum keyword in C++ and mainly use to assign names to integral constants.
//Example of C++ program to demonstrate enum
#include<iostream>
using namespace std;

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

int main(){
  enum week day;
  day = Sunday;
  
  cout<<day;

  return 0;
}
Output:
0

4. Union
Union and Structure in C++ have almost the same features and both are user-defined data types. The only difference between them is that Union shares the same memory location. 
Example: Any change you will made in value x will be reflected in value y.
union test{
  int x, y;
};

5. Typedef
Typedef is used to explicitly define any new name to our existing data type. Example:
typedef long int ll;
ll num = 3439;


Datatype Modifiers in C++

A modifier is used to change the basic meaning of our built-in data types like int, float, double, and char so they fit precisely in various situations. There are four types of modifiers available in C++ and these are short, long, signed, and unsigned

Below is the list of datatypes and the amount of memory they required with the range of values that particular datatypes allow. 

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

⚡ Please share your valuable feedback and suggestion in the comment section below or you can send us an email on our offical email id ✉ algolesson@gmail.com. You can also support our work by buying a cup of coffee ☕ for us.

Similar Posts

No comments:

Post a Comment


CLOSE ADS
CLOSE ADS