data types in c++ |
We need variables to store data and perform operations while writing code. The data could be of any type like integer, character, decimal and boolean, etc. When we declare a variable, we actually reserve space in memory to store data. Now how can we tell the compiler which type of data we want to store? How can a compiler identify what should be the size of the reserved space? We need to specify the data type of the variable to let the compiler know what sort of data we want to store.
In short all the above-stated modifiers can be used with int. Signed and Unsigned modifiers can be applied to char as well. Long can also be used with double.
Conclusion:
In this post, I have explained Data types in c++ and I hope everything is clear now. You can ask your queries in the comment section and I will try to reply as soon as possible.
What is the data type?
The data type of a variable is actually the type of data a variable can store. If one tries to store data in a variable other than its data type, the compiler will give an error. For example, you cannot store a character in a variable of type integer.Types of data types available in c++
We can divide data types in c++ into three different categories.
- Built-in data types.
- Abstract data types
- derived data types
What are the built-in data types?
The data types that are already defined and available to use for the declaration of a variable are called built-in data types. Basic built-in data types are as follows.
Integer data type has the capability to store integer or whole numbers. Fractional or decimal numbers are not allowed. Its keyword is int.
Floating point data type can store fractional or decimal values. Keywords are float and double. Float can store up to 7 floating point digits while double can store 15 digits.
Boolean data type is capable of storing either true or false. Its keyword is bool. It has many uses in coding that we will discuss later in this post.
Character data type is able to store characters or their ASCII values. It can only store one character like "A". In order to store a word we need character array or string data type.
What are modifiers in c++?
Modifiers are the keywords used with data types to modify their size and storing capability. There are some commonly used modifiers.
- Long keyword is used when we want to store large values. It can be used with int and bool.
- Short keyword is used when we wish to store small values or data. It can only be applied to int.
- Signed keyword is used when we want to store both negative and positive values. e.g. All integers are signed by default in c++. It can be used with int and char.
- Unsigned modifier is used to stored non-negative values like zero or positive values. It can be used with int and char.
What Are Abstract or user-defined data types(ADTs)?
User-defined data types are data types defined by the user himself to hold data describing the object's attributes and state.
Some user-defined data types available in c++ are
- Class
- Structure
- Enumeration
- typedef definition
Sometimes we face circumstances where we need to deal with multiple data types. For example, we want to store data of students. In this example, we need to store student's names, marks, and grades. It is a good idea to create or define a user-defined data type to hold data of students. It can be done by using structure or class in c++. Student's names, marks, and grades are data members of our user-defined data types. It can be accessed through a student's class object either directly or indirectly. We will discuss ADTs later in detail.
What are derived data types?
Data types derived from pre-defined data types are called derived data types.they are as follows
- Functions
- Arrays
- Pointers
- References
Example Program
c++ program to calculate acceleration
#include<iostream> using namespace std; int main() { float v, u, t, acc; /* v = initial velocity, u = final velocity t = time acc= acceleration */ cout<<"Enter Initial Velocity"<<endl; cin>>v; cout << "Enter final Velocity" << endl; cin >> u; cout << "Enter time" << endl; cin >> t; // formula acc = ((u - v) / t); cout<<"acceleration"<<acc; system("pause"); return 0; }
Conclusion:
In this post, I have explained Data types in c++ and I hope everything is clear now. You can ask your queries in the comment section and I will try to reply as soon as possible.
0 Comments