Data types in C++ programming

Spread the love

Data types in C++ programming The compiler only knows this by the data type. What type of data is stored in the key variables And the compiler judges the memory space.

In which variable how much memory space to assign.

Just like we create variables to store data in C language.

In the way, we create data variables in C ++. These data variables are called data types.

INDEX –

  1. Types of data types
  2. Primary/Basic Data Types
  3. Integer data type
  4. Float data type
  5. Character data type
  6. Double data type
  7. Void data type
  8. User-Defined Data Types
  9. Class data type
  10. Enum data type
  11. Structure data type
  12. Union data type
  13. Derived Data Types
  14. Arrays
  15. Single/One Dimensional Array
  16. Multi-Dimensional Array
  17. Pointers
  18. Referencing Operator
  19. Dereferencing Operator
  20. Conclusion
  21. Frequently Asked Questions

Types of data types

The data types in C ++ are divided into 3 parts.

Primary /Basic Data Types – This type of data types are mostly used. And this data type is found in all programming languages.

User-Defined Data Types – These data types are called class. Which we create ourselves. And access.

Derived Data Types – It contains a combination of several types of data types. And these types of data types are called Derived Data Types

1.Primary /Basic Data Types – 

Basic data types are those data types.

Which are commonly used data types in c++

And these types of data types are found in all programming languages.

these are the basic data types.

Which are mandatory to use. The list of basic data types is given below.

1. Integer data type – 

Only data numbers are stored in this type of data type.

integer data type in decimal or Numbers or numbers that come after the decimal are not stored.

The integer data type only stores whole numbers.

The data type found in an integer data type is represented by the table below.

Integer Data Types, Storage Size, and Range

RangeStorage SizeRange
int (16-bit)2 Bytes-32,768 to 32,767
int (32-bit)4 Bytes-2,147,483,648 to 2,147,483,647
int (64-bit)8 Bytes-9,223,372,036,854,775,807 to 9,223,372,036,854,775,807
unsigned int (16-bit)2 Bytes0 to 65,535
short int2 Bytes-32,768 to 32,767
unsigned short int2 Bytes0 to 65,535
signed short int2 Bytes–32,768 to 32,767
long int4 Bytes–2,147,483,647 to 2,147,483,647
unsigned long int4 Bytes0 to 4,294,967,295
signed long int4 Bytes–2,147,483,648 to 2,147,483,647

These data types are used according to their size.

is the type of int datastore you want to store. You can store them according to their size.

Ex- int num = 7000;

2. Floating Point data type –

In the Floating-point Data Type, we use the ‘float’ keyword to declare the variable.

Floating-point Data Type is of 4 bytes.

Numbers are as well stored by floating-point data types. Which contains a decimal. Floating-point data types are of 3 types.

RangeData TypesRange
float4 Bytes1.2E-38 to 3.4E+38

The range and size of the data types vary according to the table given above. You can use any data type according to you.

Example –

Float num =4.10;

3. Character data type –

These data types stores the character in the variables is if you want to store any character from A to Z. you can use character data types.

In the Character Data Type, we use the ‘char’ keyword to declare the variable.

One can declare only one character. for eg. ‘H’

multiple characters mean to print the entire string, use double quotes (” “). For

 eg. char arr [10] = “Hello”;

Character Data Type is of 1 byte.

Storage SizeStorage SizeRange
char1 Byte-128 to 127
unsigned char (16-bit)1 Byte0 to 255
signed char1 Byte–128 to 127

we create Character data types. we always define the data in single coats or double coats. An example is given below.

char num = ‘t’;

char num1 = “T”;

4. Double data type –

In double data type, we use the ‘double’ keyword to declare the variable.

Double Data Type is of 8 bytes.

There is no difference between the double and floating-point data types.

RangeStorage SizeRange
double8 Bytes2.3E-308 to 1.7E+308
long double10 Bytes3.4E-4932 to 1.1E+4932

5. Void data type –

These data types are used for the function and parameter/argument of the function.

It does not return any value. void means the null value void has no value.

2. User-Defined Data Types –

Structure and union in C language are a user-defined data type. This type of data type is completely allowed in  C ++. And at the time, C ++ provides us the capabilities to create some new user define data types which are known as object-oriented programming.

1. Class data type –

Class is an object-oriented programming base data types. And Class is a user-defined data type. The Class is like structure. we create variables as well function. And they are accessed by objects. An example is given below.

class num { your variables and function };

2. Enum data type –

Enum data type is user define data types. To create an enum data type, we use the enum keyword. And we create Enum data types.

numbers are attached to each of the names. using names like enum keyword automatically assigns names from 0 to values. An example is given below.

enum num {values1, values2, values3};

3. Structure data type –

Structure This is a collection of different data types.

the structure is to be used, use the keyword ‘struct’.

The Structure is similar to an array.

An Array is a collection of similar data types and Structure is a collection of different data types.

The declaration of every variable in the structure is called ‘member’.

Structure allocates different memory for each member.

Syntax for Structure

struct structure_name{ 

    type member 1; 

    data_type member 2; 

    ——————

    ——————

    data_type memeber n; 

};

4. Union data type –

The Union is a collection of different data types.

the union is to be used, use the keyword ‘union’.

The Union is similar to this structure.

The declaration of every variable made in a union is called ‘member’.

Union does not allocate different memory for each member.

Union members share the same memory location.

a member in a union is larger, it is the size of the entire union.

The Syntax for Union

struct union_name{ 

    data member 1; 

   data member 2; 

    ——————

    ——————

    data_type member n; 

}; 

read – Control Statements

3. Derived Data Types –

Derived data types are data types. Through whom we perform some operations in the program. Derived data types are derived from basic data types.

There are 3 types of derived data types found in C ++.

1. Array – 

This is a collection of variables of the same data type.

Whatever the data type of Array can be. The element of Array is the first, its memory address is the lowest.

Array’s variables are stored at the nearest Memory Location.

Initialization of Array starts with ‘0’.

students have to store their names, have to make different variables.

for eg.

char stud1 = “TOSIF”, stud2 = “RAZA”;

Array has to create only one array_variable for all these names.

for eg.

char arr [12] = {“TOSIF”, “RAZA”};

There are two types of array

1.  Single/One Dimensional Array –

Some memory is required to store every data types and variables in Programming Languages ​​or Computer, integer requires 2 Bytes, Character needs 1 Byte.

In C++ Programming, Array is a data type that can initialize multiple values ​​in an array_variable. Array’s base address 0 starts with this index number.

Syntax

Data_type Array_name[size];

ex- int num[] = { 2, 4, 5, 3, 2 };

In this way, elements make their place on memory. If you have to store the value of these variables then in the single index mean int num [2] = 5; You can store the value through any such loop.

In this program Array’s variable has been initialized along with its values.

2. Multi-Dimensional Array –

Multi-Dimensional Array is type of Array in C++ Programming.

Syntax :-

Date_type array_name [row_size] [column_size];

for e.g. num [4] [3] = { {1,2,3} {4,5,6} {7,8,9} {10,11,12} };

This example has 4 rows and 3 columns,

In this way, 4 rows and 3 columns mean 4 rows * 3 columns = 12 values ​​memory.

In this program, i and j used these two variables to display the initialized values ​​in the output, and with the help of these two variables two for loop means the nested loop is repeated.

The i variable is declared to display the values ​​of the rows and the j variable is declared to display the values ​​of the columns.

2. Pointer – 

each variable has a memory address and to see the memory address, the programmer has to put ‘&’ (address operator) the variable

The value of Variable is accessed with the help of this memory address, this is called ‘Pointers’.

Pointer holds the address of variable.

Each variable has an address. the variable declares, only it stores at a memory location.

Ex- int a = 10;

Syntax for Pointer Declaration

data_type *pointer_name;

Example for Pointer Declaration

int *ptr1; // integer pointer variable

char *ptr2; // character pointer variable

float *ptr3; // float pointer variable

double *ptr4; // double pointer variable

you want to hold the address of an integer data type variable, the pointer will be of the same data type as the data type variable, meaning the variable is int, the pointer will be int. if the variable is char the pointer variable will be char. The pointer does not hold any value, only holds its address and from that address, the value of the variable with the pointer is printed.

There are two types of pointer

1. Referencing Operator

Referencing means holding the address of other variable.

To hold the address of each variable whose address is to be held and the pointer variable to hold, the data type of both of them must be the same.

Example – int a = 10;

int *ptr;

ptr = &a;  //address of a stored in ptr

2. Dereferencing Operator

Using asterisk (*) in dereferencing, in pointer it is called Dereferencing Operator.

In dereferencing, the value stored in the pointer is accessed.

int a = 10;

int *ptr;

ptr = &a;

int b = *ptr; // ptr means dereferencing

            cout<<“Value of a : “<

Output –

Value of a : 10

3. Functions

source – javatpoint

Conclusion –

Primary /Basic Data Types, User-Defined Data Types, Derived Data And these types all of them Syntax, examples, I gave you above.

Friends, reading this post completely, you must have understood data types in c++ programming. you must have liked it. I try to describe to you in simple language, maybe you must have understood it in this post. I have covered Topics you do not need to read any other post. If you liked this article, then definitely share it with your friends. And there is some doubt. Then comment I will definitely answer your comment.

Frequently Asked Questions

1. What are the data types in C++?

Data types define the type of data a variable can hold, for example, an integer variable can hold to categorized in three groups: Built-in, user-defined, and Derived.

2. What are  the User-defined Data Types?

 In C++ we can also define our data types like a class or a structure. These are called user-defined types.

3. What are Primitive Data Types?

 This type of data type is mostly used. And this data type is found in all programming languages

4.  What are Derived types?

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

5. What are Data Type Modifiers?

Primitive data types that store different values use entities called data type modifiers to modify the length of the value that they can hold.

7. What is type Typedef?

By using the typedef declaration, we create an alias or another name for the data type. Then we can use this alias to declare more variables.

8. Why pointers are used in C++?

One reason to use pointers is so that a variable or an object can be modified in a called function. In C++ it is a better practice to use references than pointers. This makes it easy to change the way the calling function receives the value without having to modify the semantics of passing it.

9. What is the use of array?

An array is a data structure that contains a group of elements. Typically these elements are all of the same data type, such as an integer or string. Arrays are commonly used in computer programs to organize data so that a related set of values can be easily sorted or searched.

Leave a Comment