C++ Data Types

Primitive Built-in Data Types

Any programming language has built in data types. The data types are used to create variables or your new data types. A variable is a amount of memory that has its own name and value. That’s why it is important to know the built in data types, their properties, diapason and size. The official Microsoft site is offering the following information about C++ built in data types:

C++ Built-in Data Types

 Type Name Bytes Range of Values
 Int 4 2,147,483,648 to 2,147,483,647
 unsigned int 4 0 to 4,294,967,295
 bool 1 false or true
 char 1 128 to 127
 signed char 1 128 to 127
 unsigned char 1 0 to 255
 short 2 32,768 to 32,767
 unsigned short 2 0 to 65,535
 Long 4 2,147,483,648 to 2,147,483,647
 unsigned long 4 0 to 4,294,967,295
 long long 8 9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
 unsigned long long 8 0 to 18,446,744,073,709,551,615
 float 4 3.4E +/- 38 (7 digits)
 double 8 1.7E +/- 308 (15 digits)
 long double same as double Same as double
 wchar_t 2 0 to 65,535

Now let’s understand, what this table means.

The first column is the name of the built in data type. The second column specifies how many bytes of memory are allocated to a variable of such a type. These values can be different for different computers and it’s absolutely normal because it depends on the computer’s architecture. The last column is the range of possible values for this data type. For example, for unsigned char the range of values is from 0 to 255. It means that the value of unsigned char variable can’t be more than 255 and less than 0.


User defined Data Types

1. typedef Declarations

You can define your own name for a built in data type. For this purpose you have to use typedefkeyword. The declaration of a new name for a built in type can be done in the following way:

typedef BuiltInDataType NewName;

What does it mean? The line above can be understood as “We want to set and use NewName forBuiltInDataType”. For example:

typedef float MyFloat;

Above line means “We can treat word MyFloat as a synonym for type float”. In other words, we can create a variable of type float by using data type MyFloat as shown below.

float fAmount;
MyFloat fAmount; //Same like 'float fAmount;'

2. enum Keyword

Another way of creating our own data types using enumerations. An enumeration is a set of possible values. It is declared by using enum keywords. The declaration of an enum type looks like below:

enum  EnumName { list of possible values };

Let’s look for an example of enum:

//gives "USA" the value 0, "UK" the value 1 and "France" the value 2
enum country {USA, UK, France};

Try It

#include <iostream>
using namespace std;

//gives "USA" the value 0, "UK" the value 1 and "France" the value 2
enum country {USA, UK, France}; 

//below function takes countryCode and prints the associated country name
void PrintCountry(int countryCode)
{ 
 	switch(countryCode)
 	{ 
 		case USA:
    	cout << "Country selected is USA" << endl;
    	break;
    	case UK:
    	cout << "Country selected is UK" << endl;
    	break;
    	case France:
    	cout << "Country selected is France" << endl;
    	break;		  
	}
}

int main()
{
        //Calling PrintCountry() function
        PrintCountry(0); //will print 'Country selected is USA'
        PrintCountry(1); //will print 'Country selected is UK'
        PrintCountry(2); //will print 'Country selected is France'
}
//Calling PrintCountry() function
PrintCountry(0); //will print 'Country selected is USA'
PrintCountry(1); //will print 'Country selected is UK'
PrintCountry(2); //will print 'Country selected is France'

This line means that we have created a new enumeration type “country”. The all possible values of this enumeration type are “USA”, “UK” and “France”.


Why to use enum ? Benefits of enum

  1. Easier to change the values in future
  2. Reduces errors due to mistyping numbers
  3. Easy to read code so less chances of errors
  4. Code will have forward compatibility when we want to change enumeration values in future

We use enum when we want a readable code that uses constants. Enum makes our program easier and clearer to read. By default enum values will be of type int. The integer values can be positive or negative and can be non-unique. Any non-defined enumerators will be assigned one greater value than the previous enumerator automatically.

//below enum shows what values will be assigned in enumerator

enum Languages
{
	CPP = -5,
	<a class="wpil_keyword_link" href="https://tutorialcup.com/java" target="_blank" rel="noopener noreferrer" title="JAVA"><a class="wpil_keyword_link" href="https://tutorialcup.com/java" target="_blank" rel="noopener noreferrer" title="JAVA">JAVA</a></a>, // assigned value = -4
	PHP, // assigned value = -3
	HTML = 6,  
	SQL = 6, //yes we can have same non-unique value
	CSHARP // assigned value = 7
};
enum Languages
{
	CPP = -5,
	JAVA, 
	PHP, 
	HTML = 6,  
	SQL = 6, 
	CSHARP 
};
Translate »