Variables In C++

Declaration of variables.
C requires all the variables to be defined at the beginning of a scope. But c++ allows the declaration of variable anywhere in the scope. That means a variable can be declared right at the place of its first use. It makes the program easier to understand.

In order to use a variable in C++, we must first declare it specifying which data type we want it to be. The syntax to declare a new variable is to write the specifier of the desired data type (like int, bool, float…) followed by a valid variable identifier.
Ex.: int a;
float mynumber;

These are two valid declarations of variables. The first one declares a variable of type int with the identifier a. The second one declares a variable of type float with the identifier mynumber. Once declared, the variables a and mynumber can be used within the rest of their scope in the program. If you are going to declare more than one variable of the same type, you can declare all of them in a single statement by separating their identifiers with commas.
Ex.: int a, b, c;

This declares three variables (a, b and c), all of them of type int, and has exactly the same meaning as: int a; int b; int c;

The integer data types can be char, short, long. Integer data type can be either signed or unsigned depending on the range of numbers needed to be represented. Signed types can represent both positive and negative values, whereas unsigned types can only represent positive values (and zero). This can be specified by using either the specifier signed or the specifier unsigned before the type name.

Ex.: unsigned short int Number;
signed int Balance;

By default, if we do not specify either signed or unsigned most compiler settings will assume the type to be signed, therefore instead of the second declaration above we could have written:
int Balance;
with exactly the same meaning (with or without the keyword signed). An exception to this general rule is the char type, which exists by itself and is considered a different fundamental data type from signed char and unsigned char, thought to store characters.You should use either signed or unsigned if you intend to store numerical values in a char-sized variable. short and long can be used alone as type specifiers. In this case, they refer to their respective integer fundamental types: short is equivalent to short int and long is equivalent to long int. The following two variable declarations are equivalent:
short Year; short int Year;

Scope of variables:
All the variables that we intend to use in a program must have been declared with its type specifier in an earlier point in the code, like we did in the previous code at the beginning of the body of the function main when we declared that a, b, and result were of type int.

A variable can be either of global or local scope. A global variable is a variable declared in the main body of the source code, outside all functions, while a local variable is one declared within the body of a function or a block. Global variables can be referred from anywhere in the code, even inside functions, whenever it is after its
declaration. The scope of local variables is limited to the block enclosed in braces ({ }) where they are declared. For example, if they are declared at the beginning of the body of a function (like in function main) their scope is between its declaration point and the end of that function.

In the example above, this means that if another function existed in addition to main, the local variables declared in main could not be accessed from the other function and vice versa.

Dynamic initialization of variables.
In c++, a variable can be initialized at run time using expressions at the place of declaration. This is referred to as dynamic initialization.
Ex int m = 10;
Here variable m is declared and initialized at the same time.

Reference variables
i) A reference variable provides an alias for a previously defined variable.
ii) Example:-If we make the variable sum a reference to the variable total, then sum & total can be
used interchangeably to represent that variable.
iii) Reference variable is created as:- data type &reference name = variable name;
Ex int sum = 200; int &total = sum; Here total is the alternative name declared to represent the variable sum. Both variable refer to the same data object in memory.
iv) A reference variable must be initialized at the time of declaration.
v) C++ assigns additional meaning to the symbol & here & is not an address operation.The notation int & means referenceto int. A major application of reference variable is in passing arguments to functions.

Ex void fun (int &x) // uses reference
x = x + 10; // x increment, so x also incremented.
int main( )
{int n = 10;
fun(n); // function call

When the function call fun(n) is executed, it will assign x to n i.e. int &x = n;
Therefore x and n are aliases & when function increments x. n is also incremented. This type of function call is called call by reference.

Introduction to strings:
Variables that can store non-numerical values that are longer than one single character are known as strings. The C++ language library provides support for strings through the standard string class. This is not a fundamental type, but it behaves in a similar way as fundamental types do in its most basic usage.

A first difference with fundamental data types is that in order to declare and use objects (variables) of this type we need to include an additional header file in our source code: and have access to the std namespace (which we already had in all our previous programs thanks to the using namespace statement).

The following initialization formats are valid for strings:
string mystring = “This is a string”;
string mystring (“This is a string”);
Strings can also perform all the other basic operations that fundamental data types can,
like being declared without an initial value and being assigned values during execution.