x

C++ Data types & Modifiers

Prev     Next

What are Data types?

Data types are used to define the variables that the same of type data it can store in memory. The data types determine the type of data to be stored in memory. The data types are used to represent the different values to be stored in the variable. The variable is a name that refers the memory location which means whenever you create a variable you reserve the space in the memory and based on the data type of the variable the operating system allocates the memory.


For example :

So, in the above example the variable name is ‘a’ and the data type assigned to this variable is integer which means the variable ‘a’ can store only the integer values.

Types of Data types

In C++ the data types are divided into two types.

Primitive Data types :

The primitive data types are built-in data types or you can say predefined data types and they can be used by user to declare variables.

The primitive data types are :

  • Integer
  • Character
  • Floating Point
  • Double Floating Point
  • Boolean
  • Void

Let us discuss these data types briefly.

Integer

For the integer data type ‘int’ keyword is used. This data type requires 4 bytes of memory space and it ranges from the minimum value is -2,147,483,648 and the maximum value is 2,147,483,647, both the values are inclusive.

Let us have a look at the example :

Character

The character data type is used to store characters or a single character. For character data type ‘char’ keyword is used. This data type requires 1 byte of memory space and it ranges from -128 to 127 or 0 to 255.

Let us have a look at the example :

Floating Point

This data type is used for storing single precision floating point values or decimal values. For floating point data type ‘float’ keyword is used. This data type requires 4 byte memory space.

Let us have a look at the example :

#include<iostream.h>#include<conio.h>void main(){ clrscr(); float fl; cout<<“\n\nEnter a floating-point number: “; cin>>fl;getch();}


Double Floating Point

This double floating point data type is used to store double precision floating point values or decimal values. For double floating point data type ‘double’ keyword is used. This data type requires 8 byte of memory space.

Let us have a look at the example :

Boolean

This data type is used for storing Boolean or logical values. The Boolean variable can store either true or false value. For this data type ‘bool’ keyword is used.

Let us have a look at the example :

Void

The void data type refer the set of empty values. This data type is used with those function which does not returns a value.

Let us have a look at the example :

#include<iostream.h>#include<conio.h>void main(){ clrscr(); float fl; cout<<“\n\nEnter a floating-point number: “; cin>>fl;getch();}

What are data type modifiers?

The data type modifiers are used with built-in data types to modify the length of data which a particular data type can hold.

Some of the data type modifiers available in C++ are :

  • Signed
  • Unsigned
  • Short
  • Long

With integer base data types you can use signed, unsigned, long and short modifiers. And with char you can use signed and unsigned whereas with double data type you can use long. The signed and unsigned modifiers can also be used as prefix to long or short modifiers.

Data Type / Size(in bytes) Range
short int / 2 -32,768 to 32,767
unsigned short int / 2  0 to 65,535
 unsigned int / 4  0 to 4,294,967,295
 int / 4  -2,147,483,648 to 2,147,483,647
 long int / 4  -2,147,483,648 to 2,147,483,647
 unsigned long int / 4  0 to 4,294,967,295
 signed char / 1  -128 to 127
 unsigned char / 1  0 to 255
 float / 4
 double / 8
 long double / 12
 wchar_t / 2 or 4  1 wide character

Let us have a look at the example :

Prev     Next



Like it? Please Spread the word!