Tutorial Study Image

C++ Data Types and Modifiers


August 4, 2022, Learn eTutorial
1554

With the help of the example program, you will learn the fundamental data types used in C ++, such as character, integer, and decimal. You will also learn about the C ++ modifiers that are used to change the data types.

How do we define data types in C++?

Data types determine the type of data that a variable may hold, for example, an integer variable can hold integer data, a character type variable can hold character data, and so on.
All variables use data-type during declaration to limit the type of data that can be stored. Thus, we can conclude that data types are employed to inform variables about the kinds of data they can hold. Every time a variable is declared in C++, the compiler allows some Memory space for that variable based on the given data type. Different amounts of memory are needed for various types of data.

The programmer can choose the data type best suited to the requirements of the application because C++ provides a wide range of data types. The size and types of values that must be saved are specified by data types. Although C++ instructions are the same across all platforms, the storage representation and machine instructions used to modify each data type vary from one machine to the next.

The data types supported by C++ are as follows:

  1. Primary or Built-in or Fundamental data type
  2. Derived data types
  3. User-defined data types
     
Datatypes in C++

Datatypes in C++

C++ data types are broadly classified into three categories:

  1. Primary or Built-in or Fundamental data type

    These data types are built-in or predefined data types that can be used directly by the user to declare variables. The example includes int, char, float, bool, etc.

    Among the primitive data types offered by C++ are:

    • Integer
    • Character
    • Boolean
    • Floating Point
    • Double Floating Point
    • Valueless or Void
    • Wide Character
  2. Derived data types

    Derived Data Types are data types that are derived from primitive or built-in data types. These are classified into four types:

    • Function
    • Array
    • Pointer
    • Reference
  3. User-defined data types

    These data types are defined by the user. As an example, in C++, you can define a class or a structure. The following user-defined datatypes are available in C++:

    • Class
    • Structure
    • Union
    • Enumeration
    • Typedef defined Datatype

The primitive data types offered by C++ are discussed in this tutorial.

  1. Integer:

    • The keyword for integer data types is int.
    • Integers typically take 4 bytes of memory space and range from -2147483648 to 2147483647.
    
    int salary = 65000;
     
    
  2. Character

    • Character data types are mainly used in order to store characters.
    • The character data type is denoted by the keyword char.
    • Characters typically take up 1 byte of memory space and range from -128 to 127 (or 0 to 255).
    
    char alphabet = 'a';
     
    
  3. Boolean

    • Logic or Boolean values are stored using the boolean data type.
    • A Boolean variable has two possible values: true and false.
    • The word bool is used to describe the Boolean data type.
    
    double test = false;
     
    
  4. Floating Point

    • Single-precision floating-point values or decimal values are stored using the floating-point data type.
    • The floating-point data type's keyword is float. Float variables typically take 4 bytes of memory space.
    
    char average= 56.32;
     
    
  5. Double Floating Point

    • Double-precision floating-point values or decimal values are stored using the double-precision floating-point data type.
    • double is the keyword for the double floating-point data type.
    • Double variables typically take 8 bytes of memory space.
    
    double volume = 165.61524;
     
    
  6. Void:

    • The word void signifies "without any value."
    • A valueless entity is represented by the void data type.
    • The data type void is used for functions that do not return any values.

    Note: We cannot declare variables for the void type. We can declare void type function which is not return any values.

  7. Wide Character :

    • A wide character data type is a character data type that is larger than an 8-bit character data type.
    • Wchar_t represents this type. Typically, it is 2 or 4 bytes long.
    
    wchar_t test = L'?'  // storing Hebrew character;
     
    

Here is an example that will output the correct size of various data kinds on your computer.


#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

Datatype Modifiers

Datatypes in C++

Datatype modifiers, as the name implies, they are used with built-in data types to modify or change the length of data that a particular data type could hold or store.

The following data type modifiers are available in C++:

  • Signed
  • Unsigned
  • Short
  • Long

The table below highlights the altered size and range of built-in datatypes when combined with type modifiers:

The values listed above may differ from compiler to compiler. 

The size of all data types can be displayed by using the sizeof() function and passing the datatype's keyword as an argument to this function, as demonstrated below:

The range of fundamental data types is defined in the header file <limits.h>. The minimal value for unsigned modifiers is zero. As a result, no macro constants are provided for the unsigned minimum value.

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

Let's see some examples.


long a = 3623232;
long int b = 1645342;
long double c = 213434.56343;
short d = 3434233; // Error! out of range
unsigned int e = -5;    // Error! can only store positive numbers or 0
 

Macro Constants

Macro Constants are used to find the sizes of the integral types like a character, short, integer, long integer, long long integer. These macro definitions allow you to declare constant values that will be used throughout your code. Macro definitions are not variables and cannot be changed by your program code in the same way that variables can.

Macro constant Description Value
CHAR_BIT It returns the number of its in a char object. 8
SCHAR_MIN It returns the minimum value of a signed char object. -128
SCHAR_MAX It returns the maximum value of a signed char object. 127
UCHAR_MAX It returns the maximum value of an unsigned char object. 255
CHAR_MIN It returns the minimum value of a char object. 0 or SCHAR_MIN
CHAR_MAX It returns the maximum value of a char object SCHAR_MAX or UCHAR_MAX
MB_LEN_MAX It returns the maximum number of bytes in a multibyte character, for any locale 1 or greater
SHRT_MIN It returns the minimum value of a signed short int object. -32768
SHRT_MAX It returns the maximum value of a signed short int object. 32767
USHRT_MAX It returns the maximum value of an unsigned short int object. 65535
INT_MIN It returns the minimum value of a signed int object. -32768 or -2147483648
INT_MAX It returns the maximum value of a signed int object. 32767 or 2147483647
UINT_MAX It returns the maximum value of an unsigned int object. 65535 or 4294967295
LONG_MIN It returns the minimum value of a signed long int object. -2147483648 or -9223372036854775808
LONG_MAX It returns the maximum value of a signed long int object. 2147483647 or 9223372036854775807
ULONG_MAX It returns the maximum value of an unsigned long int object. 4294967295 or 18446744073709551615
LLONG_MIN It returns the minimum value of a signed long long int object. -9223372036854775808
LLONG_MAX It returns the maximum value of a signed long long int object. 9223372036854775807
ULLONG_MAX It returns the maximum value of an unsigned long long int object. 18446744073709551615

The actual value is determined by the specific system and library implementation, but it must represent the limitations of these kinds in the target platform. LLONG MIN, LLONG MAX, and ULLONG MAX are defined for libraries that adhere to the C standard from 1999 or later (which includes just the C++ standard after 2011: C++11).

C++ Program to Determine the Data Type Range Using Macro Constants


// C++ program to sizes of data types
#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

Derived Data Types

Data types that are derived from Primary data types are derived types. For example arrays, pointers, function types, structures, etc.

We will learn about these derived data types in Next tutorials.