Sunday, November 25, 2012

Variable in C

Variable in C


First thing in any programming language is Variable because in any program of any language
we must require variable for keeping the data and proceeding it further so let's understand What variable is...


A variable is named location of data having one address provided by compiler. In other word we can say variable is container (storage area) of data.

What is Data ?

Any thing which we can store ,can be taken as data like (any number,any character, any string ,any file/img etc.)
 
In real world we have used various type containers for specific purpose. For example we have used suitcase to store
clothes, match box to store match sticks etc. In the same way variables are used to store
data.

For example integer variables are used to store integer kind of value ,char variables is used to store characters etc. On the basis of how many data a variable will store, we can categorize the all C variable in three groups.

(a)Variables which can store only one data at time. Example: integer variables, char variables, pointer variables etc.

(b)Variables which can store more than one data of similar type at a time. Example: array variables

(c) Variables, which can store more than one value of dissimilar type at a time. Example: structure or union variables.

Properties of variable in c:

Every variable in C have three most fundamental attributes. They are:

1. Name

2. Value

3. Address
Name of a variable:

 Every variable in C has its own name. A variable without any name is not possible in C.
 Most important properties of variables name are its unique names.
Not two variables in C can have same name with same visibility.
if at all we dare to have the same name of two variable compiler will confuse while processing its data
so it will throw compile time error.
 

For example:


#include<stdio.h>
int main()
{
    auto int a=5;   //Visibility is within main block
    static int a=10; //Visibility is within main block

/* Two variables of same name */
    printf("%d",a);
    return 0;
}


Output: compilation error




But it is possible that two variable with same name but different visibility. In this case variable name can access only that variable which is more local. In C there is not any way to access global variable if any local variable is present of same name. For example:

(a)

#include<stdio.h>
int a=50; //Visibility is whole the program

int main()
{
    int a=10; //Visibility within main block
    printf("%d",a);
    return 0;
}

Output: 10


(b)

#include<stdio.h>

int main()
{
    int a=10; //Visibility within main block.
    {
        a+=5; //Accessing outer local variable a.
        int a=20; //Visibility within inner block.
        a+=10; //Accessing inner local variable a.   
        printf(“%d”,a);//Accessing inner local variable a.
    }

    printf(“%d”,a); //Accessing outer local variable a.
    return 0;
}

Output: 30 15

Address of the variable we need not to worry about that because it automatically allots by the compiler
we can not predict it...although we can also play with addresses once we enter into pointer concept(Back Bone) of C Laungauge...

No comments:

Post a Comment