C++ Data Types

Learn All About Data Types In C++ With Examples.

In this Complete C++ Training Tutorials, we will discuss data types in C++ in this tutorial.

We have already seen identifiers that are used to identify various entities in C++ by name. Apart from the identifiers, we also know that the variable store's information or data.

In order to associate data with the variable, we also need to know what data will we associate exactly i.e. whether variables are storing only alphabets or numbers or both. In other words, we need to restrict the data or information that is to be stored in a variable.

DATA TYPES IN C++

This is exactly where the data type comes into the picture. We can say that data types are used to tell the variable what type of data it should store. Based on the data type assigned to a variable, the operating system allocates memory and decides what type of data is to be stored in the variable.

Types Of Data

C++ supports two types of data to be used with its programs.

  • Primitive/Standard data types
  • User-defined data types.

Given below is the pictorial representation of the data types in C++.

C++ data types

Primitive or Standard Data Types

Primitive data types are the built-in types, that C++ language provides. We can directly use them to declare entities like variables, constants, etc. Alternatively, we can also call them as pre-defined data types or standard data types.

Following are the various primitive data types that C++ supports with their corresponding keywords:

  • Integer => int
  • Character => char
  • Floating Point =>float
  • Double Floating Point => double
  • Boolean => bool
  • Void or Valueless type => void
  • Wide Character => wchar_t

User-defined Data Types

In C++ we can also define our own data types like a class or a structure. These are called user-defined types.

Various user-defined data types in C++ are listed below:

  • Typedef
  • Enumeration
  • Class or object
  • Structure

Out of these types, the class data type is used exclusively with object-oriented programming in C++.

Primitive Data Types

The following table shows all the primitive data types supported by C++ along with its various characteristics.

Data TypeC++ KeywordValue type
CharactercharCharacter(ASCII values)
IntegerintNumeric whole numbers
Floating pointfloatDecimal values with single precision
Decimal pointdoubleDouble precision floating point values
BooleanboolTrue or false
voidvoidValueless (no value)
Wide characterwchar_tCharacter including Unicode strings

Data Type Modifiers

Primitive data types that store different values use entities called data type modifiers to modify the length of the value that they can hold.

Accordingly, the following types of data modifiers are present in C++:

  • Signed
  • Unsigned
  • Short
  • Long

The range of the data that is represented by each modifier depends on the compiler that we are using.

The program below produces the various sizes of different data types.

#include<iostream>
using namespace std;
 
int main()
{
cout<<"Primitive datatypes sizes: "<<endl;
    cout << " short int: " << sizeof(short int)
<< " bytes" << endl;
    cout << " unsigned short int: " << sizeof(unsigned short int)
<< " bytes" << endl;
    cout << " int: " << sizeof(int)
<< " bytes" << endl;
    cout << " unsigned int: " << sizeof(unsigned int)
<< " bytes" << endl;
    cout << " long int: " << sizeof(long int)
<< " bytes" << endl;
    cout << " unsigned long int: " << sizeof(unsigned long int)
<< " bytes" << endl;
    cout << " long long int: " << sizeof(long long int)
<< " bytes" << endl;
    cout << " unsigned long long int: " << sizeof(unsigned long long int)
<< " bytes" << endl;
    cout << " char: " << sizeof(char)
<< " byte" << endl;
    cout << " signed char: " << sizeof(signed char)
<< " byte" << endl;
    cout << " unsigned char: " << sizeof(unsigned char)
<< " byte" << endl;
    cout << " float: " << sizeof(float)
<< " bytes" <<endl;
    cout << " double: " << sizeof(double)
<< " bytes" << endl;
    cout << " long double: " << sizeof(long double)
<< " bytes" << endl;
    cout << " wchar_t: " << sizeof(wchar_t)
<< " bytes" <<endl;
 
return 0;
}

Output:

Primitive datatypes sizes:

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

Screenshot for this output is given below.

Output - Primitive datatypes

As we see, using the size of the operator, we can get the maximum size of data that each data type supports.

All these data types and their corresponding sizes can be tabularized as below.

Data TypeBit widthRange
char1 byte127 to 127 or 0 to 255
unsigned char1 byte0 to 255
signed char1 byte127 to 127
int4 bytes2147483648 to 2147483647
unsigned int4 bytes0 to 4294967295
signed int4 bytes2147483648 to 2147483647
short int2 bytes32768 to 32767
unsigned short intRange0 to 65,535
signed short intRange32768 to 32767
long int4 bytes2,147,483,647 to 2,147,483,647
signed long int4 bytessame as long int
unsigned long int4 bytes0 to 4,294,967,295
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

This is all about primitive data types in C++.

User-defined Data Types

These data types as the name itself suggest are defined by the user itself. As they are user-defined, they can be customized as per the requirements of the program.

Typedef

By using the typedef declaration, we create an alias or another name for the data type. Then we can use this alias to declare more variables.

For Example, consider the following declaration in C++:

typedef int age;

Through this declaration, we have created an alias age for the int data type.

Hence, if we want to declare anything similar, then we can use the alias instead of the standard data type as shown below:

age num_of_years;

Note that alias is just another name for the standard data type, it can be used in a similar way like the standard data types.

Enumeration

The enumeration in C++ is a user-defined data type which consists of a set of values with corresponding integral constants for each value.

For Example, we can declare the days of the week as an enumerated data type as shown below:

enum daysOfWeek {Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday};

By default, integral constants for each of the enum value starts with zero. So ‘Sunday’ has value 0, ‘Monday’ has 1 and so on.

However, we can also change the default values from the start of in-between as follows:

enum daysOfWeek {Sunday, Monday, Tuesday=5, Wednesday, Thursday, Friday, Saturday};

Here, Sunday will have value 0, Monday will have value 1, and Tuesday will have value 5 that we have assigned. After Tuesday, remaining values will have 6, 7, and so on in continuation with the previous value (in this case 5).

Let us make use of this enum that we declared earlier in the following program:

#include<iostream>
using namespace std;

enum daysOfWeek {Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday};
 
int main()
{
    daysOfWeek today;
    today = Thursday;
    cout<<"This is day "<<today<<" of the week";
    return 0;
}

Output:

This is day 4 of the week

Screenshot for the same is given below

enum_output

The above program is self-explanatory. We have defined the enum and then we create its type variable to output the day of the week.

Class

In C++, we can define yet another user-defined type named “Class”. Class is nothing but a collection of objects. Class acts as a blueprint for an object and using the class definition we can design various real-time problems.

For Example, consider a class named “Student” which will be defined as follows:

class student{
                  char* name;
                  int age;
                  public:
                               void printDetails()
                               {

                                             cout<<”Name: “<<name;
                                             cout<<”Age: “<<age;
}
};

Once we have defined this class, we can use the class name to declare variables of type class. These variables of type class are nothing but objects.

So we declare an object of type student as follows:

student s1;

s1.printDetails();

As shown above, we can also access the members of this class which are public. We will see the classes and objects in detail when we cover object-oriented programming in C++.

Structure

A structure in C++ is similar to that in C>. In fact, the concept of structure in C++ is directly picked up from C language. As a class, the structure is also a collection of variables of different data types. But class has both variables and methods that operate on these variables or members as we call them.

Structures, on the other hand, have only variables as its members.

We can define a structure person as follows using the struct keyword:

struct employee{
                   Char name[50];
                    Float salary;
                    Int empId;
};

Once the structure is defined we can declare a variable of type struct as follows:

Employee emp;

Then we can access the members of the structure using the structure variable and member access operator (dot Operator).

Conclusion

We will learn more about structure and class and the differences between them once we start with the object-oriented programming in C++.

In our upcoming tutorial, we will explore C++ variables and its other aspects.

=> Check The In-Depth C++ Training Tutorials Here