Data Types in C++

In this tutorial, we will know what is data types in C++ Programming Language. What is Data type modifiers? How to find the size of data types in C++ and the size of a variable in C++.

Data types define the type of data a variable can store. Whenever we declare a variable it reserves some memory locations to store various information. Based on the data type of a variable, the operating system allocates memory and decides what can be stored in the reserved memory.

Mainly Data Types in C++ can be devided into two types:-

  1. Primitive data type:- These are predefined data types and can be used directly to declare a variable. The primitive data types are:- Integer, Character, Boolean, Floating point, Double Floating Point, Void or Valueless, and Wide Character.
Primitive Data TypesKeyword
Valuless or voidvoid
Booleanbool
Characterchar
Wide Characterwchar_t
Integerint
Floating Point float
Double Floating Point double

2. User-defined data type:- These data types are defined by the user to declare the variable. Structure, union, enum are examples of user-defined data types.

Data Type Modifiers

These are used to modify the length of data that a particular data can hold. These modifiers are:- Signed, Unsigned, Short and Long

Examples are signed int, unsigned int, short int, long int, signed short int, unsigned short int. Note that these modifiers do not use with float and double (exception is long double).

If signed and unsigned not written with data types then by default it is signed data types. So, int and signed int are same.

Size of Data Types in C++

The size of data types is dependent on the compiler or you can say that the system architecture i.e. 32-bit compiler or 64-bit compiler. The size of data type int is 2 byte in 32-bit architecture or 4 bytes in 64-bit architecture. Another factor on which the size of data type depends is the compiler on which you perform any program.

For finding the size we need a sizeof() function. sizeof() function find the size in bytes. Binary 0 or 1 takes 1-bit space and 1 byte is equal to 8 bits. Using sizeof() we can find the size of data-types or a variable also. endl used to create a new line, we can also use ‘\n’.

C++ Program to Find the Size of Data Types

#include <iostream>
using namespace std;
int main()
{
  cout << "Size of char = " << sizeof(char) << " byte" << endl;
  cout << "Size of wchar_t = " << sizeof(wchar_t) << " bytes" << endl;
  cout << "Size of short int = " << sizeof(short int) << " bytes" << endl;
  cout << "Size of int = " << sizeof(int) << " bytes" << endl;
  cout << "Size of unsigned int = " << sizeof(unsigned int) << " bytes" << endl;
  cout << "Size of signed int = " << sizeof(signed int) << " bytes" << endl;
  cout << "Size of long int = " << sizeof(long int) << " bytes" << endl;
  cout << "Size of Float = " << sizeof(float) << " bytes" << endl;
  cout << "Size of double = " << sizeof(double) << " bytes" << endl;
  return 0;
}

Output:-

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

The size of signed and unsigned data types will be the same Because these modifiers change the range of data types not the size of data types. But, the Size of short and long data types may be different.

Size of a Variable in C++

#include <iostream>
using namespace std;
int main()
{
  int x=10;
  char ch='A';
  cout << "Size of Variable x = " << sizeof(x) << " bytes" << endl;
  cout << "Size of Variable ch = " << sizeof(ch) << " bytes" << endl;
  return 0;
}

Output:-

Size of Variable x = 4 bytes
Size of Variable ch = 1 bytes

The Size of variable and data types are the same because we use variables to store the data.

The Range of Data Types in C++

TypeTypical Bit
Width
Typical Range
char1 byte-127 to 127 or 0 to 255
unsigned char1 byte0 to 255
signed char1 byte-127 to 127
short int2 bytes-32768 to 32767
unsigned short
int
2 bytes0 to 65,535
signed short int2 bytes-32768 to 32767
int4 bytes-2147483648 to 2147483647
unsigned int4 bytes0 to 4294967295
signed int4 bytes-2147483648 to 2147483647
long int4 bytes-9223372036854775808
to 9223372036854775807
unsigned long int4 bytes0 to 18446744073709551615
signed long int4 bytes-9223372036854775808
to 9223372036854775807
float4 bytes+/- 3.4e +/- 38 (~7 digits)
double8 bytes+/- 1.7e +/- 308 (~15 digits)
long double8 bytes+/- 1.7e +/- 308 (~15 digits)
wchar_t2 or 4 bytes1 wide character

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *