Data types in C++

Nitin
3 min readAug 17, 2021

Last time we discussed variables in C++. If you haven’t read about it read it here: https://niftynitin.medium.com/variables-in-c-5e71fe59d765

Source : Unsplash

A data type is a restriction on the variable for the type of data it can store.

All data types in C++ is divided into mainly three types:

  1. Primitive data types: These data types are predefined i.e. they can be directly used by the programmers to create variables. Integer, floating-point, character, boolean, void, and wide character are the primitive data types available in C++.
  2. Derived data types: The derived data types are derived from the primitive data types. Function, arrays, pointers are derived data types.
  3. User-defined data types: These data structures are aggregations of existing data types. These are custom declared by the user. Class, structure, union are examples of user-defined data types.

Let’s discuss each one of the primitive data in detail. The non-primitive data types will be discussed later.

int — It is used to store integer type of data. eg. 5, -7, 154, -40, etc. int typically takes 4 bytes of data(it may vary from compiler to compiler though).

float — floating-point data is used to store numbers with fractional parts or decimal values. It takes 4 bytes of memory.

double — double floating data is used to store double-precision floating values. It usually takes 8 bytes of memory.

character — It is used to store characters like ‘a’, ‘7’, ‘$’ etc. It takes 1 byte of memory.

boolean — A boolean datatype stores logical boolean values(1 for true and 0 for false).

Data type modifiers

Modifiers are special keywords available that change the properties of datatypes by modifying the range and the memory allocated to the data types.

The following modifiers are available in c++:
1. signed
2. unsigned
3. short
4. long

Signed, unsigned, short, long can be used for integers. Signed and Unsigned can be used for characters and long can be applied for double.
Moreover, signed and unsigned can be used as a prefix to short and long integers.

The table below shows the different access modifiers along with the size and range. The size may vary from compiler to compiler.

...................................................................
: Data Type : Size : 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 : 8 : 0 to 4,294,967,295 :
: long long int : 8 : -(2^63) to (2^63)-1 :
: unsigned long long int : 8 : 0 to 18,446,744,073,709,551,615 :
: signed char : 1 : -128 to 127 :
: unsigned char : 1 : 0 to 255 :
: float : 4 : :
: double : 8 : :
: long double : 12 : :
:........................:......:.................................:

The following code demonstrates the use of different data type modifiers.

Output:

References:

--

--

Nitin

I am a CS graduate and an ML enthusiast. My purpose here is to guide beginners in their programming and Machine learning journey.