Data Types and Type Compatibility

C++ supports a wide variety of data types and the programmer can select the data type appropriate to the needs of the application. Data types specify the size and types of value to be stored. However, storage representation and machine instructions to manipulate each data type differ from machine to machine, although C++ instructions are identical on all machines. C++ supports the following data types:

  • Primitive Data Types
  • Derived Data Types
  • User Defined Data Types

1. Primitive Data Types: These data types are built-in or predefined data types and can be used directly by the user to declare variables. example: int, char, float, bool, etc. Primitive data types available in C++ are: 

  • Integer: The keyword used for integer data types is int. Integers typically require 4 bytes of memory space and range from -2147483648 to 2147483647. 
  • Character: It is used for storing characters. The keyword used for the character data type is char. Characters typically require 1 byte of memory space and range from -128 to 127 or 0 to 255.  
  • Boolean: It is used for storing Boolean or logical values. A Boolean variable can store either true or false. The keyword used for the Boolean data type is bool.
  • Floating Point: It is used for storing single-precision floating-point values or decimal values. The keyword used for the floating-point data type is float. Float variables typically require 4 bytes of memory space. 
  • Double Floating Point: It is used for storing double-precision floating-point values or decimal values. The keyword used for the double floating-point data type is double. Double variables typically require 8 bytes of memory space. 
  • Valueless or Void: Void means without any value. void data type represents a valueless entity. A void data type is used for those function which does not return a value.

2. Derived Data Types: The data types that are derived from the primitive or built-in datatypes are referred to as Derived Data Types. These can be of four types namely: 

  • Function: A function is a block of code which only runs when it is called. You can pass data, known as parameters, into a function. Functions are used to perform certain actions, and they are important for reusing code: Define the code once, and use it many times.
  • Array: Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value. To declare an array, define the variable type, specify the name of the array followed by square brackets and specify the number of elements it should store: int num[4];
  • Pointer: A pointer is a variable that stores the memory address as its value. A pointer variable points to a data type (like int or string) of the same type, and is created with the * operator. The address of the variable you're working with is assigned to the pointer. 
  • Reference: A reference variable is an alias, that is, another name for an already existing variable. Once a reference is initialized with a variable, either the variable name or the reference name may be used to refer to the variable.

3. Abstract or User-Defined Data Types: These data types are defined by the user itself. Like, as defining a class in C++ or a structure. C++ provides the following user-defined datatypes: 

  • Class
  • Structure
  • Union
  • Enumeration
  • Typedef defined Datatype: C++ allows you to define explicitly new data type names by using the keyword typedef. Using typedef does not actually create a new data class, rather it defines a name for an existing type. This can increase the portability(the ability of a program to be used across different types of machines; i.e., mini, mainframe, micro, etc; without much changes into the code)of a program as only the typedef statements would have to be changed. Using typedef one can also aid in self-documenting code by allowing descriptive names for the standard data types.

C++ Type compatibility

In C++ language the types of values must be the same for complete compatibility, or a cast must be applied. These restrictions in c++ are necessary in order to support function overloading where two functions with the same name are distinguished using the type of function arguments another major difference is the way char constants are stored. in C programming, they are stored as ints, and therefore is equivalent to sizeof(int) in c language, in c++ language, however, char is not promoted to the size of int and therefore sizeof(‘y’) equals sizeof(char). 

C++ is very strict with regard to type compatibility. Type compatibility is very close to implicit or automatic type conversion. The type compatibility is being able to use two types being able to substitute one for the other without modification and together without modification.

The type compatibility is categorized into following three types by the compiler:

1. Assignment compatibility: If the one type of variable assigned to another type variable is different, It will results into loss of value of assigned variable if the size of the assigned variable is large than the size of variable to which it is assigned.

                       For example              float f1=12.5;                  int i1=f1;

This assigning of variable f1 float value to i1 int type will result in loss of decimal value of f1. However, this type type compatibility will not show any type error but it might give a warning “possible loss of data”.

2. Expression compatibility

Consider following example

int num=5/2;

cout<< num;

Here in the above example the result will be 2 because The actual result of 5/2 is 2.5 but because of incompatibility there will be loss of decimal value

3. Parameter compatibility

Due to incompatibility when we pass the values from calling to called function in type of actual parameter and formal parameters loss of data occurs. For example,

void display(int num) {          cout << ”num=” << num;           }

void main()   {                   display(9.25);                    }

Here output will be num=9 due to incompatibility in actual and formal parameter type.

No comments:

Post a Comment