Getting started in C++ : Variable and Data Types || How is Data Stored || Operators
Part 2
Variable and Data Types
As we know in refrigerated storage, if we want to ice cube then we require to store at a freezing temperature and for normal cold water at a cool temperature. It's similar in data type, for example if we require to store integer then we store in int data type and for character we store in char data type.
Let's look how do we use data type in code
#include <iostream>
using namespace std;
int main()
{
int a = 5;
int b = 10;
int c = a + b;
cout << c << endl;
}
we earlier discussed about why we use include and int main if you have no idea then click here
Time to break down (int a = 5), here a is just a variable name of a storage and we sorted there(in storage) a integer value 5 and for integer value we use int data type. So, compiler can able to understand and compile code without any error. Similar in case of b also in c to but there is we are initializing two int value in a storage as addition and it will give integer value after addition. So, we used int value for c.
special case in character, we use single quotes ('c') and it is a single character but in case of "c" it's just string .
( single quotes identify a single character, while double quotes create a string literal. 'a' is a single a character literal, while "a" is a string literal containing an 'a' and a null terminator (that is a 2 char array).)
we use "sizeof( )" to find the size of storage, let's look in code and try to run.
#include <iostream>
using namespace std;
int main()
{
int a = 5;
int b = 10;
int c = a + b;
cout << c << endl;
char d = 'x';
cout << d << endl;
int size = sizeof(a);
cout << size << endl;
}
NOTE : we can use any name for storage but make sure not start with integer. for ex, we can use as _1b or b1 but not 1b
#include <iostream>
using namespace std;
int main()
{
int _1b = 8; //this is right
int b1 = 4; //this is right
int 1b = 12; //this is wrong way
cout << _1b << " "<< b1 <<" "<< 1b;
}
Taking Input
suppose we require to ask some value form user and for that there is cin build in C++ lets look on code for better understanding then we will go through code break down
#include <iostream>
using namespace std;
int main()
{
int a, b;
cin >> a;
cin >> b;
// cin >> a >> b ; we can use like that to
int c = a + b;
cout << c << endl;
The "c" in cin refers to "character" and "in" means "input". Hence cin means "character input"
cin : cin is an inbuilt object that used to accept the input from the standard input device . The extraction operator(>>) is used along with the object cin for reading inputs(in simple word it's takes input from user)
How is Data Stored
Integral types must be stored as binary, so you have one bit per binary digit. With very few exceptions, all of the bits are significant (although this is not required, and there is at least one machine where there are extra bits in an int ).
ASCII characters are stored by their value. But storing 'A' (65 = 0x41) may be different than storing 65 itself, and how it is done depends on your machine architecture. A char can be stored with a single byte, while a int will be at least 2 bytes (more commonly 4 bytes in modern machines), and so these may be stored differently
In case of float(ex : 2.34) it has 4 bites float some are fix for mantissa and some are fix for exponent, so we do store mantissa and exponent in different different way.(just taking about +ve float)
Time to look out on one code
#include <iostream>
using namespace std;
int main()
{
int a;
char c;
a = 105;
c = a;
cout << c << endl;
}
Here we get output "i", the reason is we did assigned c as a char data type and we learned how char value storage dose works. Don't be confuse c and a is just variable name we can assigned as any name like Joe and Mike
Try to figure out the output yourself
#include <iostream>
using namespace std;
int main()
{
int Joe = 25;
char Mike = 'm';
int output = Joe + Mike;
cout << output << endl;
}
Operators
An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations. C++ is rich in built-in operators and provide the following types of operators
In athematic operator, if we do multiply, divide, subtract or add with same data type then we will get same data type value otherwise it's depends on which data type give more significant value.
I will add more within few time, Till then happy reading...............!