Explanation of Local and Global Variables with Example

Engg.... | 04:32 | | | Best Blogger Tips
Local and Global variables:
Local and Global variables:

Local variables: 
Variable whose existence is known only to the main program or functions are called local variables. Local variables are declared with in the main program or a function.

Global variables: 
Variables whose existence is known to the both main() as well as other functions are called global variables. Global variables are declared outside the main() and other functions.
The following Program illustrates the concept of both local as well as global variables.

Statement of C Program: This program does not accept anything from the keyboard. It initialises the variables a to 10 outside the main() function and value().

#include<stdio.h>
int a = 10;                                 /* global declaration */
main()
{
int b;
printf("  = %d\n" , i);
b = value(i)
printf(" j = %d\n");
}                                                              /* End of main() */
                    /* Function to compute value */
int value(i)
int i;
{
int c;
c = i + 10;
return(c);
}                                           /* End of Function */

Output:
 i = 10
 b = 20

Explanation:
The statement int  i = 10;   that appears before the main() is a global declaration. The value of i is accessed by the main program as well as the function value(). The variable c is local to the function value(). It has no existence in the function value(). The variable b is also local to the main() but has no scope in the function value().

                                                                  That's All
/*--------Flipper Sharing Widget ------*/ .flipper a { display:block; height:48px; width:48px; padding:0 4px; float:left; background:transparent url(http://4.bp.blogspot.com/-dlD3BDKTZjA/T9IuXkfhSeI/AAAAAAAAGno/s8BGOkjdwX0/s1600/flipper.png) no-repeat; -webkit-transition: ease-in 0.15s all; -moz-transition: ease-in 0.15s all; -o-transition: ease-in 0.15s all; -ms-transition: ease-in 0.15s all; transition: ease-in 0.15s all; cursor:pointer; } .flipper a.googleplus { background-position: 0px -348px; } .flipper a.googleplus:hover { background-position: 0px -406px; } .flipper a.pinterest { background-position: 0px -464px; } .flipper a.pinterest:hover { background-position: 0px -522px; } .flipper a.delicious { background-position: 0px 0px; } .flipper a.delicious:hover { background-position: 0px -58px; } .flipper a.digg { background-position: 0px -116px; } .flipper a.digg:hover { background-position: 0px -174px; } .flipper a.stumbleupon { background-position: 0px -812px; } .flipper a.stumbleupon:hover { background-position: 0px -870px; } .flipper a.technorati { background-position: 0px -928px; } .flipper a.technorati:hover { background-position: 0px -406px; } .flipper a.twitter { background-position: 0px -928px; } .flipper a.twitter:hover { background-position: 0px -986px; } .flipper a.facebook { background-position: 0px -232px; } .flipper a.facebook:hover { background-position: 0px -290px; } .flipper a.reddit { background-position: 0px -580px; } .flipper a.reddit:hover { background-position: 0px -638px; } .flipper a.rss { background-position: 0px -696px; } .flipper a.rss:hover { background-position: 0px -754px; } .Pleaseshare{ margin:10px 0px; color:#333333; font-weight:bold; font-size:20px; font-family:sans-serif; }
Please Share it! :)
Do you like this Story?

Get Free Email Updates Daily!

Follow us!

7 comments:

  1. Very good post mr admin. Very good variable explanation. For more programming examples on C programming visit :-> theluckyshow.com

    ReplyDelete
  2. There is no statement that appears as i=10 before the main func. That is u have given it as a=10, I think there is a correction that should be made over there..

    ReplyDelete
  3. This blog awesome and i learn a lot about programming from here.The best thing about this blog is that you doing from beginning to experts level.

    Love from

    ReplyDelete
  4. Thanks for sharing such fantastic stuff.it is a very informative and i have enjoyed this.
    https://play.google.com/store/apps/details?id=ias.upsc.ibps.rrb.exam&rdid=ias.upsc.ibps.rrb.exam

    ReplyDelete


  5. Very informative article.Thank you author for posting this kind of article .

    http://www.wikitechy.com/view-article/variables-declaration-in-c-programming-with-example-and-explanation


    Both are really good,
    Cheers,
    Venkat

    ReplyDelete


  6. Very informative article.Thank you author for posting this kind of article .

    http://www.wikitechy.com/view-article/variables-declaration-in-c-programming-with-example-and-explanation


    Both are really good,
    Cheers,
    Venkat

    ReplyDelete