Tech Study

C++ Data Types

C++ Data Types decide the type and size of a variable.

We often need to use various variables to store various information while writing programs in any language. Variables generally are reserved memory locations to store values into. This ultimately means that when someone creates a variable then they reserve some space in memory.

One can usually like to store information having different data types like character, wide character, integer, floating point, double floating point, boolean, etc. The operating system usually allocates memory and decides what can be stored in that reserved memory based on the data type of our declared variable.

Primitive Built-in C++ Data Types:-

The programmers of C++ are provided with a rich assortment of built-in as well as user-defined data types. Following is the table which enlists seven basic C++ data types –

Data Type Keywords in the language
Boolean bool
Character char
Integer int
Floating point float
Double floating point double
Valueless void
Wide character wcha

 

 

Some of the basic data types in c++ can generally be modified using one or more of following type modifiers −

  • signed
  • unsigned
  • short
  • long

Below we can find a table that shows the variable type, how much memory that type takes to store the value in memory, and what can be the maximum and minimum value which we will be able be store in such type of variables.

Type Typical Bit Width Typical Range
Data type:-char 1byte -127 to 127 or 0 to 255
Data type:-unsigned char 1byte 0 to 255
Data type:-signed char 1byte -127 to 127
Data type:-int 4bytes -2147483648 to 2147483647
Data type:-unsigned int 4bytes 0 to 4294967295
Data type:-signed int 4bytes -2147483648 to 2147483647
Data type:-short int 2bytes -32768 to 32767
Data type:-unsigned short int 2bytes 0 to 65,535
Data type:-signed short int 2bytes -32768 to 32767
Data type:-long int 8bytes -9223372036854775808 to 9223372036854775807
Data type:-signed long int 8bytes same as long int
Data type:-unsigned long int 8bytes 0 to 18446744073709551615
Data type:-long long int 8bytes -(2^63) to (2^63)-1
Data type:-unsigned long long int 8bytes 0 to 18,446,744,073,709,551,615
Data type:-float 4bytes
Data type:-double 8bytes
long double 12bytes
wchar_t 2 or 4 bytes 1 wide character

The size of variables is supposed to vary from those shown in the above table, as it completely depends on the compiler and the computer you are using.

Below we can find the example, which is going to produce correct size of various data types on the computer.

#include <iostream>

using namespace std;

int main() {

   cout << "Size of the datatype char : " << sizeof(char) << endl;

   cout << "Size of the datatype int : " << sizeof(int) << endl;

   cout << "Size of the datatype short int : " << sizeof(short int) << endl;

   cout << "Size of the datatype long int : " << sizeof(long int) << endl;

   cout << "Size of the datatype float : " << sizeof(float) << endl;

   cout << "Size of the datatype double : " << sizeof(double) << endl;

   cout << "Size of the datatype wchar_t : " << sizeof(wchar_t) << endl;

   

   return 0;

}

The following result, which can vary from machine to machine, is produced when the above code is compiled and executed :-

Size of the datatype char : 1

Size of the datatype int : 4

Size of short the datatype int : 2

Size of the datatype long int : 4

Size of the datatype float : 4

Size of the datatype double : 8

Size of the datatype wchar_t : 4

Java Final keyword

Introduction : java final keyword The final keyword present in Java programming language is generally used for restricting the user. …

Read more

C++ Memory Management: new and delete

C++ Memory Management We know that arrays store contiguous and the same type of memory blocks, so memory is allocated …

Read more