What is a variable.....what is the difference between variable and data type | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What is a variable.....what is the difference between variable and data type

4th Aug 2019, 7:50 AM
Shivayogi K v
Shivayogi K v - avatar
3 Answers
+ 4
int Carrie=10 //Carrie is a variable with int data type and holds 10. double pi=3.14 //pi is a variable with double data type and holds 3.14. https://www.sololearn.com/learn/CPlusPlus/1606/
4th Aug 2019, 7:57 AM
你知道規則,我也是
你知道規則,我也是 - avatar
+ 3
A variable is associated with its type and is only capable of storing values of that type, they work like storage box's specifically made to store a certain type of item, say in real life you tried to store a big microwave in a box that only stores small cups the box would be the variable of item "type smallcups" it has no idea how to store a microwave which is a diffrenet type of item.
4th Aug 2019, 8:22 AM
D_Stark
D_Stark - avatar
+ 3
very good description from D_Stark. I want to add a comment to special variables, that are data structures. compared to variables, data structures can contain more than one value. So instead of using 10 different variables for 10 names, we can use a data structure to hold all of them. Date structures can be lists, tuples, sets or dictionaries. Each of them can contain basic data types like string, int, float as well as other objects like data structures. These can be mixed like shown here: my_list = [1, 'hello', 27.13, [1, 2, 3], (10, 15), {'a': [100, 255]}] The advantage of data structures that you can iterate them by using a for loop.
4th Aug 2019, 10:40 AM
Lothar
Lothar - avatar