Data types in C++ are keywords that are used to specify the type of the data we use in our C++ programs.
There are many data-types available in C++. They are classified under two categories:
- Built-In or Fundamental Data types
- Derived or User-Defined Data types
Built-in data types
- Built-in data types are the most basic data-types in C++.
- The term built-in means that they are pre-defined in C++ and can be used directly in a program.
- char, int, float and double are the most common built-in data types.
- Apart from these, we also have void and bool data types.
Derived data types
- User-defined data types are those which are first declared and defined by the user before use.
- They are composed of built-in data types.
- Examples are : array, structure, union, class, pointers etc
In this post, we will be dealing with built-in data types. User-defined data types will be presented in detail in further posts.
The various built-in data types are:
1. int data type
The int data type is used for integers. Integers are numbers with no decimal point. For example : 24, -90, 1234, 0 etc.
2. float data type
This data type is used for numbers that have a decimal point which are commonly called floating-point numbers. For example: 76.45, 12.4, -90.6 etc.
3. double data type
This data type is also used for floating-point numbers.
But the difference is that the range and precision of this data type is greater than that of float. In other words, it is used for large floating-point numbers, the ones that do not fit in the scope of the float data type.
4. char data type
This data type is used for characters. It can be used for any valid character in C++ and not just alphabets.
Here is a special thing about this data type: characters are stored in memory using their ASCII codes which are numeric i.e. integers. Thus, char type is basically the same as int data type.
Now let us talk about two special data types: bool and void
bool data type:
bool refers to boolean. This data type has only two values: true and false.
void data type:
The word void means empty. So it has no value. This is used as return type for functions that do not return a value.
We hope you are clear with these fundamental data types in C++. Do let us know your queries in comments below.