Data Types in C++

A data type specifies the type of data that a variable can store. The data types decide the type of data and the memory location required for storing that type of data in the memory.

Data types in C++
Data types in C++

USER DEFINED

  1. Structure
  2. Union
  3. Class
  4. Enumeration

1.Structure

  • A Structure is a collection of simple variables. The variables in a structure can be of different types: Some can be int, some can be a float, and so on.
  • The data items in a structure are called the members of the structure
  • A structure is a collection of data, while a class is a collection of both data and functions
 struct structure_name
{
data_type variable_name;
data_type variable_name;
-
-
}; 

Learn Structure in Details:

Click ME

2.Union

  • Union is a collection of multiple data elements that can be of different data types. But, only one of these data item can be stored in the union variable
  • The memory space required to be store one variable of union is equal to the memory space required by the largest element in the union
  • Syntax of Union is given below:
 union union_name
{
data_type variable_name;
data_type variable name;
-
-
}; 

Learn Union in Details:

Click ME

3.Class:

  • Class is the language element in C++ most important to the support object-oriented programming(OOP)
  • A class defines the properties and capacities of an object
  • A class definition specifies the name of the class and the names and types of the class members
  • The definition begins with the keyword class followed by the class name
  • The data member and methods are then declared in the subsequent code block

Learn Union in Details:

Click ME

4.Enumeration:

  • These are used to contain values that are not limited to the values that are possible for fundamental data types. For example, if there could be data type months, its values can be January, February, March, April, May, June, July, August, September, October, November or December. Similarly, if there could be red, blue, green, cyan, yellow, pink, white, black, etc.
  • Enumerated types of work when you know in advance a finite list of values that a data type can take on
  • Thus, such special things can be created using the “enum”.
  • The syntax of creating an enum is as given below:
 enum enum_name {<value1>,<value2>...}; 

Learn Enumeration in Details:

Click ME

BUILT-IN TYPE CONTAIN

  1. Integer types
  2. Floating Point types
  3. Void types
  4. Character types

1.Integer types

  • The Int to hold numbers in the range from -2,147,483,648 to 2,147,483,647
  • Integers space required in memory is 2 bytes
  • C++ has three classes of integer storage, namely short int, and long int, in both signed and unsigned forms.
  • Integers exist in C by keyword int
  • C++ defines these types so that they can organize from the smallest to the largest

2.Floating Point types

  • Floating point(or real) number stored in 32 bits with 6 digits of precision( It is used to describes the number of digits that are used to express a value. )
  • Floating point number defined in C by the keyword float
  • A double data types number uses 64 bits giving a precision of 14 digits known as double-precision numbers
  • To extend the precision further, we may use long double which uses 80 bits.
  • Remember that double type

3.Void types

  • The void types have no values
  • void types used to specify the type of functions
  • When a function does not return any value to the calling function
  • It can also play the role of a generic type

4.Character types

  • A single character can be defined as a character(char) type data
  • Characters are usually stored in 8 bits(one byte) of internal storage
  • A qualifier signed or unsigned may be explicitly applied to char
  • While unsigned chars have values between 0 to 255, signed chars have values from -128 to 127

For Example :

#include<stdio.h>
#include<conio.h>
 void main()
 {
 int a;
 float b;
 clrscr();
 printf("Enter a float number:");
 scanf("%f",&b);
 a=(int)b;    //using type casting
 printf("The integer part of the number is:%d",a);
 getch();
 }

OUTPUT:

 Enter a float number : 3.14
 The integer part of the number is :3 

Derived Type

  1. Array
  2. Function
  3. Pointer

1.Array

  • It is a collection of multiple data of the same data type. for example, we can have an array of int type data or float type data, etc
  • In Array, items are accessed(read) by an index number
  • Remember, an array can have data of the same type only i.e. all elements of an array have to be of the same type only
  • We cannot have an array of a combination of different data types

Learn Array in Details:

Click ME

2.Function

  • The function is a self-contained block of statements that performs a given task.
  • Function groups a number of statements into a unit and give it a name. This unit can then be called from other parts of the program
  • Syntax of a function is given below:
return_type function_name(argument_list)
{
-
-
statements;
-
-
}

Learn Function in Details:

Click ME

3.Pointer

what are pointers for in programming languages?

  • Accessing array elements
  • Passing arguments to a function when the function needs to modify the original argument
  • Passing arrays and strings to functions
  • Obtaining memory from the system
Data_type *ptr_name;

Learn Pointer in Details:

Click ME

Translate »