C++ Data Types – GeeksforGeeks

All variables use data-type during declaration to restrict the type of data to be stored. Therefore, we can say that data types are used to tell the variables the type of data it can store. Whenever a variable is defined in C++, the compiler allocates some memory for that variable based on the data type with which it is declared. Every data type requires a different amount of memory.

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:

  1. Primary or Built in or Fundamental data type
  2. Derived data types
  3. User defined data types

Data Types in C/C++

Data types in C++ are mainly divided into three 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
  • Character
  • Boolean
  • Floating Point
  • Double Floating Point
  • Valueless or Void
  • Wide Character

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
  • Array
  • Pointer
  • Reference

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

This article discusses primitive data types available in C++. 

  • 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: Character data type 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: Boolean data type 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: Floating Point data type 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: Double Floating Point data type 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. 
  • 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. 
  • Wide Character: Wide character data type is also a character data type but this data type has a size greater than the normal 8-bit datatype. Represented by wchar_t. It is generally 2 or 4 bytes long.

The size of variables might be different from those shown in the above table, depending on the compiler
and the computer you are using.

sizeof operator — sizeof operator is used to find the number of bytes occupied by a variable/data type in computer memory. Eg:     int m , x[50];     cout<<sizeof(m); //returns 4 which is the number of bytes occupied by the integer variable “m”.     cout<<sizeof(x); //returns 200 which is the number of bytes occupied by the integer array variable “x”.     

C++




  

#include <iostream>

using namespace std;

  

int main() 

{

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

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

      

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

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

      

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

      

      return 0;

}



Output

Size of char : 1
Size of int : 4
Size of long : 8
Size of float : 4
Size of double : 8

GeeksforGeeks-CPP-Foundation-Course

Time Complexity: O(1)

Space Complexity: O(1)

Datatype Modifiers

As the name suggests, datatype modifiers are used with the built-in data types to modify the length of data that a particular data type can hold. 

Modifiers in C/C++

Data type modifiers available in C++ are: 

  • Signed
  • Unsigned
  • Short
  • Long

The below table summarizes the modified size and range of built-in datatypes when combined with the type 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

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

 

wchar_t

2 or 4

1 wide character

Note: Above values may vary from compiler to compiler. In the above example, we have considered GCC 32 bit.
We can display the size of all the data types by using the sizeof() operator and passing the keyword of the datatype, as an argument to this function as shown below: 

Now to get the range of data types refer to the following chart

Note: syntax<limits.h> header file is defined to find the range of fundamental data-types. Unsigned modifiers have minimum value is zero. So, no macro constants are defined for the unsigned minimum value.

Macro Constants

Name                                                                                                                             Expresses
CHAR_MIN                                                              Minimum value for an object of type char
CHAR_MAX                                                                Maximum value for an object of type char
SCHAR_MIN                                                             Minimum value for an object of type Signed char
SCHAR_MAX                                                                Maximum value for an object of type Signed char
UCHAR_MAX                                                                 Maximum value for an object of type Unsigned char
CHAR_BIT                                                                 Number of bits in a char object
MB_LEN_MAX                                                                 Maximum number of bytes in a multi-byte character
SHRT_MIN                                                                 Minimum value for an object of type short int
SHRT_MAX                                                                 Maximum value for an object of type short int
USHRT_MAX                                                                 Maximum value for an object of type Unsigned short int
INT_MIN                                                                 Minimum value for an object of type int
INT_MAX                                                                 Maximum value for an object of type int
UINT_MAX                                                                 Maximum value for an object of type Unsigned int
LONG_MIN                                                                 Minimum value for an object of type long int
LONG_MAX                                                                 Maximum value for an object of type long int
ULONG_MAX                                                                 Maximum value for an object of type Unsigned long int
LLONG_MIN                                                                 Minimum value for an object of type long long int
LLONG_MAX                                                                 Maximum value for an object of type long long int
ULLONG_MAX                                                                 Maximum value for an object of type Unsigned long long int

The actual value depends on the particular system and library implementation but shall reflect the limits of these types in the target platform. LLONG_MIN, LLONG_MAX, and ULLONG_MAX are defined for libraries complying with the C standard of 1999 or later (which only includes the C++ standard since 2011: C++11).

C++ Program to Find the Range of Data Types using Macro Constants

CPP




#include <iostream>

#include <limits.h>

using namespace std;

  

int main()

{

    cout << "Size of char : " << sizeof(char) << " byte"

         << endl;

    

  cout << "char minimum value: " << CHAR_MIN << endl;

    

    

  cout << "char maximum value: " << CHAR_MAX << endl;

    

    

  cout << "Size of int : " << sizeof(int) << " bytes"

         << endl;

      

  cout << "Size of short int : " << sizeof(short int)

         << " bytes" << endl;

      

  cout << "Size of long int : " << sizeof(long int)

         << " bytes" << endl;

      

  cout << "Size of signed long int : "

         << sizeof(signed long int) << " bytes" << endl;

      

  cout << "Size of unsigned long int : "

         << sizeof(unsigned long int) << " bytes" << endl;

      

  cout << "Size of float : " << sizeof(float) << " bytes"

         << endl;

     

  cout << "Size of double : " << sizeof(double)

         << " bytes" << endl;

      

  cout << "Size of wchar_t : " << sizeof(wchar_t)

         << " bytes" << endl;

  

    return 0;

}



Output

Size of char : 1 byte
Size of int : 4 bytes
Size of short int : 2 bytes
Size of long int : 8 bytes
Size of signed long int : 8 bytes
Size of unsigned long int : 8 bytes
Size of float : 4 bytes
Size of double : 8 bytes
Size of wchar_t : 4 bytes

Time Complexity: O(1)

Space Complexity: O(1)

This article is contributed by Harsh Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to [email protected]. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above.

My Personal Notes

arrow_drop_up