VC++ tutorial

Visual C++ Examples

How To Add Menu In VC++

Variables In VC++
Previous Home Next

variable are used in c++ , where we need storage for any value. variable can be declared in multiple ways each with different memory requirements and functioning.

variable is the name of memory location allocated by the compiler depending upon the data type of the variable. the set of operation can be applied to the variable. name of variable can be defined as a letter, digit, special character.

Basic types of variables

bool Stores either value true or false.
char Typically a single octet(one byte). This is an integer type.
int The most natural size of integer for the machine.
float A single-precision floating point value.
double A double-precision floating point value.

Declaration : variables are used after declaration. but in c++ can be declared in the middle of program too.

char  k;   //(declare )

Initialization: initialization means when using declare variable assigning value.

int j;      //(declaration)
j = 12;   //(initialization)

and also can be declare in a single step;

int j = 12;
int j = 1, k = 11;

when a variable is declared and not be initialized by the default it will hold a garbage value.if a variable is declared and if try do declare it again so shown a compile time error.

Scope of variable : All the variables have their area of functioning, and out of that boundary they don't hold their value, this boundary is called scope of the variable. variables can be divides into two types:

  1. Global variable: global variable which are declared and can be used throughout the lifetime of the program by any class or any function. they must be declared out side the main() function. and also can be assigned any value at any point in the program.
  2. Local variable: Local variables are the variables which exit only between the curly braces, in which its declared. Outside that they are unavailable and leads to compile time error.
  3. #include<iostream>
    	using namespace std;
    	int C, D;
    	int Mul()
    		return C * D;
    	int main()
    		int answer;
    		C = 35;
    		D = 40;
    		answer = Mul();
    		cout << C << " * " << D << " = " << answer;
    		return 0;

    you can see two global variables are declared, C and D. These variables can be used in main() and Mul(). The local variable answer can only be used in main()

Previous Home Next

Tolal:0 Click:

Show All Comments

Did not find what you were looking for leave your name and message. We will revert within 24 hours
Comment / Feedback: