What Is Variables And It's Data Types ? || Explained With Real Life Example || Variables and datatypes in C++

 

VARIABLES AND IT'S DATATYPES IN C++

By- Dhananjay Arne

What is a variable?

A variable is a space in your memory in which you can store data. Variables allow you to use that stored data in the future in your program. You can create a variable in the program and then you can use it.


Understanding Variable With A Real-Life Examples - 

 To get a more clear understanding of the variable let me explain this concept with a real-life example. As you have a kitchen in your home, in the kitchen you may have some boxes in which you can store sugar, salt, chili, etc. and if the boxes are not transparent and same then probably you put a label on it in which you write what is inside in this box, and you can use these boxes to store some kind of ingredients or masala, so you can use it in future.


So same as you are doing in your kitchen you can do this in programming also. As you have boxes in your kitchen , same in programming you have memory in your CPU. As you store some ingredients in the box, same in programming you stores some data (in binary) in memory. As you labled your boxes, same you can lable your memory in programming to recall it.



I hope till now you understood what is a varaible.


And if you want to know more about the variables then here you have  the official definition of variables - 

   
In computer programming, a variable or scalar is a storage location (identified by a memory address) paired with an associated symbolic name, which contains some known or unknown quantity of information referred to as a value.

 

How to create variables in C++?

 

#include <iostream>
using namespace std;
int main()
{
    /*
    Here we are 
    declaring a varible
    
    Syntax : 
    data_type variable_name;
    */
    int var; 
    //Assigning value in variable
    var = 22;
    //Printing var value
    cout<<var<<endl;
    return 0;
}


 

 

DATATYPES - 


 So till now you understood what is a variable and what is the work of the variable. But one thing is there as we discussed kitchen box example, then in ktichen you have a some kind of boxes category to store any ingredient like you cannot store milk in your wheat storage tank, similarly you cannot store wheat, rice in a spices box. So same in programming if you create a variable then you have to define it's type like which type of data this memory can store. So let's learn more about data types.

 WHAT IS DATATYPE? 

 Data type - as name sounds type of data. So there are many types of data we can store in a memory or now you can say in a variable. Here are the types of the data -


  • int - Integer 
  •  float - Floating-Point

  • double - Double Floating-point 
  •  char - Character 
  • bool - Boolean

  • void - Empty 




How to create variables of different data type in C++?

#include <iostream>
using namespace std;

int main()
{
    /*
    Declaring variables of different 
    datatypes
    */
   
   //Integer Data Type Variable
   int num = 22;

   //Floating Data Type Variable
   float decimal = 22.4;

   //Double Data Type Varibale
   double long_decimal = 22.44403;

   //Character Data Type Variable
   char letter = 'D';

   //Boolean Data Type Variable
   bool isRich = false;

   //Printing All The Values
   cout<<num<<endl;
   cout<<decimal<<endl;
   cout<<long_decimal<<endl;
   cout<<letter<<endl;
   cout<<isRich<<endl;
}

So that's all for this post. In future posts, you will learn more about the C++ programming language.

Hope you like this post, If you like then follow our blog and if you have any suggestion or query then you can write it in the comment section, I'll try to help you there.





Comments

Popular posts from this blog

What Is Functions In C++ ? || Explained By Examples || Function Types

Basics Of Operators||Basics of operators in C++||Basics of operators in cpp||Types of operators in C++||Arithmetic Operators||Comparision Operators||Binary Operators||Logical Operators||Bitwise Operators||Assignment Operators||Miscellaneous Operators||Bitwise Explained||Operators Precedence||

Reference and Pointers in C++