Wednesday, February 7, 2018

C++ Variables

C++ Variables


  1. A variable is a name of memory location. It is used to store data. Its value can be changed and it can be reused many times.
  2. It is a way to represent memory location through symbol so that it can be easily identified.
  3. Each variable in C++ has a specific type, which determines the size and layout of the variable's memory; the range of values that can be stored within that memory;
  4. The name of a variable can be composed of letters, digits, and the underscore character. It must begin with either a letter or an underscore. Upper and lowercase letters are distinct because C++ is case-sensitive
Variable Syntax:
datatype varname;
ex: int x;
ex: int x, y;
ex: in x=5; y=10; etc...
  • In C++, there are three ways to initialize variables.
  • The first one, known as c-like initialization (because it is inherited from the C language), consists of appending an equal sign followed by the value to which the variable is initialized:
datatype varname= initial_value;
ex: int x=10;
  • A second method, known as constructor initialization (introduced by the C++ language), encloses the initial value between parentheses ():
datatype varname (initial_value);
ex: int x(10);
  • Finally, a third method, known as uniform initialization, similar to the above, but using curly braces ({}) instead of parentheses (this was introduced by the revision of the C++ standard, in 2011):
datatype varname {initial_value};
ex: int x{10};

Types of variables:

There are mainly two types of variables those are:
  • Local Variables
  • Global Variables

Local Variables:

  • Variables defined within a function or block are said to be local to those functions.
  • Anything between ‘{‘ and ‘}’ is said to inside a block.
  • Local variables do not exist outside the block in which they are declared, i.e. they can not be accessed or used outside that block.

Global Variables:

  • As the name suggests, Global Variables can be accessed from any part of the program.
  • They are available through out the life time of a program.
  • They are declared at the top of the program outside all of the functions or blocks.

Example1:
#include<iostream>
using namespace std;
int y=15; // global variable (created outside the block)

int main(){
    int x=10; // local variable (created inside the block)
    cout << x;
    cout<< y;
    return 0;
}

Example2:
#include<iostream>
using namespace std;
int y(15); // global variable (created outside the block initialized with constructor)

int main(){
    int x{10}; // local variable (created inside the block initialized with uniform)
    cout << x;
    cout<< y;
    return 0;
}

Example3:
#include<iostream>
using namespace std;
int x=15; // global variable (created outside the block)

int main(){
    int x=10; // local variable (created inside the block)
    cout << x;  
    return 0;
}

// here both local and global variable names are same at this time by default the output will be the local variable value because it is local to this function.

Example4:
#include<iostream>
using namespace std;
int x=15; // global variable (created outside the block)

int main(){
    int x=10; // local variable (created inside the block)
    cout << ::x;  
    return 0;
}
// here both local and global variable names are same at this time if you want to print global variable value use scope resolution operator (::) before the variable name.

You can also use extern keyword to access a global variable from one program to another program.
You cannot apply extern keyword to local variables.
ex: extern int x=15; // must and should it is a global variable which means it is declared outside the block only

No comments:

Post a Comment