Posted by : Waqas Javed Tuesday, 14 October 2014


When we want to use any value or data in our program we want to save it in our memory so that it can be used again and again. For that purpose we store it in a variable. Variable is the actually the address of a particular memory where we can save our data. two location could not have same address that's why we could not create two variable with same name.

We can store create different types of variable depending upon the type of data we want to save. If we want to save a digit we will create a integer var, if we want to store a character we will create a character type var.
A variable must be declared before it could be used in our program. It could be declared by defining its type and its name its value or data is not necessary.

  type identifier = initial_value ;(int is type in below example, identifier is name and initial value may                                                    or may not be assigned as required)

1.            int a;

2.            int b = 3;

3.            int c,d;

Line 1 is declaring a integer type variable with name a and it have no value initially.
Line 2 is declaring a integer type variable with name b which have value 3 so b =3.
Line 3 is declaring a integer type two variable with name a and b. we could declare multiple var in single type to reduce our space.


Character or small integer.

signed: -128 to 127
unsigned: 0 to 255
short int
Short Integer.

signed: -32768 to 32767
unsigned: 0 to 65535

signed: -2147483648 to
unsigned: 0 to 4294967295

long int
Long integer.

signed: -2147483648 to
unsigned: 0 to 4294967295

Boolean value. It can take one of two values: true or false.

true or false
Floating point number.
+/- 3.4e +/- 38 (~7 digits)

Double precision floating point number.
+/- 1.7e +/- 308 (~15 digits)

long double
Long double precision floating point number.
. 8bytes
+/- 1.7e +/- 308 (~15 digits)

Wide character.

2 or 4 bytes

_ 1 wide character

Scope of variables

1.            #include <iostream>
2..           int a;                                               //global variable
4.            void main () {
5.                            int b,c;                              //local variable

6.            }
8.            void myFunction () {
9.                            int b,d;                           //local variable

10.          }

in line 2 we created an integer a which is global variable it scope is around the whole program any where it could be used or modified as desired. No other function could have var with name (a) because (a) is already declared at every place of function.

in line 5 the var (b) and (c) are the local copies inside main. these variable could be used every where in main function but not outside.

in line 9 we have created var (b) and (d) which are the local copies of myFuntion could only be used in this function. but not outside.

In the program you could see that we have created (b) in main and also in myFunction both are the local copies so we can create a var with same name outside the scope of it.

we can not create var (a) any where in our program because its scope is all over the program and vars with same name could not be created within its own scope.

Leave a Reply

Subscribe to Posts | Subscribe to Comments

Popular Post

Powered by Blogger.

- Copyright © BSCS -Metrominimalist- Powered by Blogger - Designed by Johanes Djogan -