codeintuition-logo

Data Types in C++

Understand what are the different data types in C++

5 minutes
Easy
Beginner

What you will learn

What is data and why is it important

What is a data type and why do we need it

Why we cannot mix different types of data

How does C++ understand data types

What are the different data types provided by C++

What are primitive and abstract data types in C++

The C++ programming language provides us with the ability to work with many different types of data. It may seem a bit cumbersome at first and it is only natural to ask why we even need data types in the first place. In this article, we will talk about data types and try to understand why not all data is the same.

What is data?

The main goal of computer programs is to make the life of a human easy. This is achieved by writing programs that will do some tasks automatically for you, instead of you or some other human doing it themselves. So the basic idea of creating any computer program is to create something that can replicate what a human does, but a lot faster and more efficiently.

Data
Data is information that has been translated into a form that is efficient for movement or processing

In order for a computer program to mimic the real world, it needs the ability to understand, store, retrieve and manipulate data just like we humans do. We will be able to build more sophisticated and complex computer programs if our programming language provides us with the ability to work with data easily. For computers, however, all data is the same. it is just a sequence of 0s and 1s for them.

What are types?

The data we work with in our daily lives can be categorised into different types. And each type has a certain set of rules governing its behaviour and interaction with other types. Let's take an example. When someone asks you your name, you write it in alphabets like "Shikhar Srivastava". When someone asks you how many movies have you watched in the past year, you might say 10. And when someone asks you what is the length of your hair, you might say "2.5 cm".

So, would it make sense if someone asks you to add 20 to your name? 

"Shikhar Srivastava" + 20 = ?

It is really absurd, the reason being that your name and a number are different types of data. 20 is a number and your name is a set of alphabets. They cannot be mixed. It would have made much more sense if someone had asked you -

  • "Shikhar Srivastava" + " how are you?" = "Shikhar Srivastava how are you?
  • 20 + 30 = 50

Now it makes much more sense. This clearly proves that the data around us is not all the same, it has different types. If you think more, you might be able to come up with all the different types of data around you.

Data types in C++

Just like the real world, computer programs must also be able to differentiate between different types of data which they will manipulate so that they don't mix our names and the number of movies we watched. This is achieved in programming languages like C++ by using the concept of data types.

data types in C++
A data type in C++ is a way of differentiating between the different types of data that is used by a C++ program. It is the basic building block of the C++ language using which the compiler can decide how to store, retrieve and manipulate the data. It also tells us about the size, range and type of data that it can handle. Every variable in C++ will always have an associated data type

Primitive Data Types

The inbuilt data types that come together with C++. These are the most fundamental data types that cannot be broken down into any other data types like numbers, characters, etc.

User Defined Data Types

These are custom data types created by users according to their needs. They are generally high-level data types that are created by mixing one or more primitive or user-defined data types like struct, class, etc

Do you want to master data structures?

Try our data structures learning path made of highly visual and interactive courses. Get hands on experience by solving real problems in a structured manner. All resources you would ever need in one place for FREE

Shikhar Srivastava

Shikhar Srivastava

London, United Kingdom