Data Types in cpp

In this tutorial, we are going to learn about Datatypes in C++. Datatypes are an important concept to learn and understand in C++.

In programming, we often deal with some kind of information in our programs, like name, age, address, salary, etc. for any kind of purpose. So, since we have different data like we can have numbers, characters, boolean values, etc. we have different datatypes for them in C++.

Data Types in cpp

First of all, what is Data? Data can be considered as some kind of information.

There are different types of data, that you would need to store like age is a number, the name is a set of characters, a salary is a number (maybe the floating point), and so on.

So, we have different data types in C++ that specify what type of data you want to store.

These are usually categorized into 3 types –

  • Built-in data types.
  • Derived data types.
  • User-defined data types.

First of all, let’s see what are the basic data types –

Built-in Data Types in CPP

some of the basic data types are –

  • int – This data type holds the integer values. And it generally takes 4 bytes of memory to store.
  • char – This data type holds characters(like alphabets, special symbols etc) . It takes 1 byte to store.
  • float – This data type holds the floating point values. It generally takes 4 bytes to store.
  • double – This data type holds the floating point values with double precision. By double precision, we mean that this data type can store more bytes in comparison to float, which makes it generally take 8 bytes to store.
  • bool – This data type is specifically used when we need to store only true or false.
  • void – This simply means nothing, it just represents a valueless entity.

We are going to store some data so we are in turn going to specify the type of data using these data types. We will be solving many programs and in almost every program, at some point, we will be using some kind of data type. So, don’t worry if you did not get everything, just wait till we start with the concept of variables, where we need to revisit the concept of data types.

There is one more data type that is used quite specifically, and it is a boolean data type, which has the keyword bool. It can store only true or false. We will be using this data type as and when we need it further.

Derived Data Types in cpp

The derived data types simply mean that they are derived from the built-in data types.

Some of the derived data types are –

  • Array – the array is a collection of data of similar data type. This is used when we need to store many values of a specific data type. We will learn more in detail about arrays as we move further.
  • Pointer – A pointer is a variable that stores the address of another variable.
  • Function – A function can be considered as a block of code, which does some particular task. There are many built–in functions as well, and we can also create our own functions.

User-Defined Data Types in c++

As the name suggests, user-defined data types are the data types that are defined by the user.

Some of the user-defined data types are-

  • class – class is a user-defined data type used in object-oriented programming. You can say that it is a model/template for creating some objects.
  • Actually, there is something called classes and objects in Object Oriented Programming (OOP). So, don’t worry, if you are not getting this, just remember about the big picture, and when you would explore about the concept of OOP in C++, you would get the story.
  • structure – This is a user-defined data type in which you can store the collection of data, of different datatypes.
  • union – This is similar to the structure. The difference between them lies in the memory allocations done to the structure data type and the union data type. We are going to see the concepts of structure and union later. Which will give you a more clear idea about these concepts.
  • Enum – This consists of named values, which represent integral constants.

Summary

In this tutorial, we have learned about datatypes in C++. At times, in our programs, we would need to deal with different types of data in our programs, and for that, we have different datatypes, that we can use. When you would perform different programs, and actually deal with some data in your programs, you would get the concept and the need.

If you find this tutorial to be interesting, you can explore our other tutorials related to C++ programming language to learn more about C++ programming language. You can also explore our other courses, related to Python, Machine learning, and Data Science, through which, you can upskill yourself.

FAQs related to datatypes in cpp

Q: What are datatypes in C++?

Ans: Datatypes in C++ can be considered representations for data in our programs. It basically gives some information about the data that we are dealing with.

Q: What is bool in C++?

Ans: bool is a keyword in C++, for the boolean datatype. Boolean means just true or false.