C++ Data Types

C++ Data Types

We would cover C++ Data Types in this lesson under the following topics:

  1. Introduction to C++ Data Types
  2. C++ Built-in Data Types
  3. Why do we need Data Types
  4. The typedef Keyword
  5. The enum Types


1. Introduction to C++ Data Types

Data types is quite easy understand but very important. For example, a digit is different from a letter. And so on.

Also, remember that C++ is a strongly typed language. So you need to be aware of the data types available.

Just to remind you, inĀ  a strongly typed language, you must specify the data type of your variables.

We discuss more on variables in the next chapter.



2. C++ Built-in Data Types

Built-in data types are also called basic data types. C++ provide a number of data types which are listed in the table below:

Data Type Keyword
Boolean bool
Character char
Integer int
Floating point float
Double floating point double
Valueless void
Wide character wchar_t


Some of these primitive data types can be modified using any of the modifiers listed below:

  • short
  • long
  • signed
  • unsigned


3. Why do we need data types?

Data types are needed to indicate how much memory is reserved for the data. You can find in the table below, each data type along with the size and range.

Data Type Width in bytes Data Range
char 1 byte -127 to 127 or 0 to 255
unsigned char 1 byte 0 to 255
signed char 1 byte -127 to 127
int 4 bytes -2147483648 to 2147483647
unsigned int 4 bytes 0 to 4294967295
signed int 4 bytes -2147483648 to 2147483647
short int 2 bytes -32768 to 32767
unsigned short int 0 to 65,535
signed short int -32768 to 32767
long int 4 bytes -2,147,483,648 to 2,147,483,647
signed long int 4 bytes same as long int
unsigned long int 4 bytes 0 to 4,294,967,295
float 4 bytes +/- 3.4e +/- 38 (~7 digits)
double 8 bytes +/- 1.7e +/- 308 (~15 digits)
long double 8 bytes +/- 1.7e +/- 308 (~15 digits)
wchar_t 2 or 4 bytes 1 wide character


But sometimes, the actual size of the may vary depending on the computer or compiler being used.

You can find out the size of each data type using the sizeof(datatype). The code below prints out the sizes of different variables.


// Program to display size of data types
// By Kindson The Genius
#include <iostream>
using namespace std;

int main() {
   cout << "Size of int: " << sizeof(int) << endl;
   cout << "Size of char: " << sizeof(char) << endl;
   cout << "Size of short int: " << sizeof(short int) << endl;
   cout << "Size of long int: " << sizeof(long int) << endl;
   cout << "Size of float: " << sizeof(float) << endl;
   cout << "Size of double: " << sizeof(double) << endl;
   cout << "Size of wchar_t: " << sizeof(wchar_t) << endl;
   return 0;


In this code, the <<endl is used to add a new line after each data type printed.

This code will give an output below:


Size of int : 4
Size of char : 1
Size of short int : 2
Size of long int : 4
Size of float : 4
Size of double : 8
Size of wchar_t : 4



4. The typedef Keyword

Interestingly, you can change the name of the data types. C++ allows you to do that using the typedef keyword.

The syntax is:

typedef type newname;


I don’t thing you’ll use this much. So don’t bother much but just know the syntax.



5. enum Types (or Enumerated types)

This is a way to provide a type name for a set of items. For example, you have a list of fruits, say, orange, banana, apple, pear. We want to give them a type called fruit. The we can achieve this using the syntax below:


enum fruit {orange, banana, apple, pear};

fruit f;

f = banana;


In this case, fruit is an enum type

Now about enum types. The list of items inside the braces have default values starting from 0. Therefore the value of orange is 0, the value of banana is 1 and so on.