Variables

A variable is used to store a value in it. Whatever value you store in a variable is stored in the memory location associated with that memory location. You assign a value to a variable with the following syntax.
We can first declare a variable type by specifying a type and then the variable name: Type variable;
Here, type is the variable type and variable is the name of the variable. Next, we can assign a value to a variable, which is done like so: Variable = value;
Now that value is assigned to the variable. Or, you can both declare the variable and assign a value to it in a single line, as follows: type variable = value;

Before you set a variable, you have to specify the variable type.You can then use the equal to (=) sign to assign the value to the variable.

Let's look at some example code:
#include <iostream.h>                                                                                                         #include <conio.h>
//
Program prints out value of n to screen
int main()
{  int n=42;
std ::cout<<"value of n is :"<<n<<std::endl;
getch();
return 0;
}

Replace the previous code with this code in Source.cpp and run the application. This is the output you should get:                                                                                                                                                                               Value of n is :42

In this program, we specify the data type as int. An int is a C++ data type that can store integers. So, it cannot store decimal values. We declare a variable called n and then we assign a value of 42 to it. Do not forget to add the semicolon at the end of the line. In the next line, we print the value to the console. Note that, to print the value of n, we just pass in n in cout and don't have to add quotation marks. On a 32-bit system, an int variable uses 4 bytes (which is equal to 32 bits) of memory. This basically means the int data type can hold values between 0 and 232-1 (4,294,967,295). However, one bit is needed to describe the sign for the value (positive or negative), which leaves 31 bits remaining to express the actual value. Therefore, a signed int can hold values between -231 (-2,147,483,648) and 231-1 (2,147,483,647).

Let‘s look at some other’s data type

• double: This is a float with more precision. It can store decimal values up to 15 decimal places.
• char: This stores integers ranging between -128 and 127. char or character variables are used to store ASCII characters such as single characters—letters, for example
• short and long: These are also integer types but these are able to store more information than just int. The size of int is system-dependent and long and short have fixed sizes irrespective of the system used.
• float: This is a floating point type. This means that it can store values with decimal spaces such as 3.14, 0.000875 and -9.875. It can store data with up to seven decimal places.
• bool: A bool can have only two values. It can either store true or false.

You also have unsigned data types of the same data type used to maximize the range of values it can store. Unsigned data types are used to store positive values. Consequently,all unsigned values start at 0.
So, char and unsigned char can store positive values from 0 to 255. Similar to unsigned char we have unsigned short, int, and long.
You can assign values to bool, char, and float as follows:

#include <iostream>
#include <conio.h>
// Program prints out value of bool, char and float to screen
int main()
bool a = false;   char b = 'b';   float c = 3.1416f;
unsigned int d = -82;
std::cout << "Value of a is : " << a << std::endl;
std::cout << "Value of b is : " << b << std::endl;
std::cout << "Value of c is : " << c << std::endl;
std::cout << "Value of d is : " << d << std::endl;
getch();
return 0;
}

This is the output when you run the application:

Value of a is: 0
Value of b is : b
Value of c is :3.1416
Value of  d is: 4294967214
Everything is printing fine except d, which was assigned -82. What happened here? Well that's because d can store only unsigned values, so if we assign it -82, it gives a garbage value. Change it to just 82 without the negative sign and it will print the correct value

Value of a is: 0
Value of b is : b
Value of c is : 3.1416
Value of  d is: 82
Unlike int, bool  stores a binary value where false is 0 and true is 1. So, when you print out the values of true and false, the output will be 1 and 0. char store characters specified with single quotation marks, and values with decimals are printed just how you stored the values in the floats. The f is added at the end of the value when assigning a float, to tell the system that it is a float and not a double.

Strings

Variables that are non-numerical are either a single character or a series of characters called strings. In C++, a series of characters can be stored in a special variable called a string. A string is provided through a standard string class.

To declare and use string objects we have to include the string header file. After #include <conio.h>, also add #include <string> at the top of the file. A string variable is declared in the same way as other variable types, except before the string type you have to use the std namespace. If you don't like adding the std:: namespace prefix, you can also add the line using the std namespace. After the #include. This way, you won't have to add the std:: prefix, as the program will understand. However, it can be printed out just like other variables:

#include <iostream>                                                                                                                                          #include <conio.h>
#include <string>

// Program prints out values to screen
int main()
{
std::string name = "The Dude";
std::cout << "My name is: " << name << std::endl;
_getch();
return 0;
}

Here is the output:
My name is : The   Dude