Explanation of Local and Global Variables with Example

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

0 Response to "Explanation of Local and Global Variables with Example"

Post a Comment

Popular Posts

Label

1 Sidebar 2 Columns 3 Columns Footer 3rdball 4 Columns Footer Alex Alvaro Antsasia Arlinadesign Averie Barry BASIC CONCEPTS Basil Bayna Fast BeautyPress BeautyTemplates Beckel Best Best - Clean Beyonce Pro BioMag BlackMag Blanter Nova Blogger Card Bloggertheme9 Blogish Bpress Brakt C LANGUAGE C PROGRAMS C++ LANGUAGE C++ PROGRAMS Carella Cellar Charlotte Chooko Lite Clara Clean Combo One Cooperate Crusade Darry Dorman Dreamway Dunia Blanter eCars EduAcademy EducationPress Elise Elvan Eva Everyday FabNews Fashion Fashion Blogger Template Fashion Gossip FastCars Fedo FlashNews Fredy Portfolio Gaming Globus GoodTime Goomsite HTML LANGUAGE iGaming Indicate Innove Insomnia InspiredNews iSoft Jasmine Kalem Landscaping Lashira Lavelle Lily Fashion Limax Linea Lionex Luminescence Lite Lumix Luxury Magazine MagazinePro MAJOR DIFFERENCES MarketingBlog Martin MaxiMag Medica Mega Melissa Mercury MeshMag Minimag Minimalist MiniNews MovieKhor msdesignbd MusicLand Neko NetZine New Spread News16 Newsflash NewsForce NewsIdea Newspaper News NewsPro Nubie Fast Odd Themes Optima PaperMag PaperMag blog Parhlo Perfection Personal Personal Blog Photosout PixelZ Power Blog Premium Themes ProfileMag Purez Rano ReadNews Real estate Remito Grid Responsive Revo Robusta Rotario Saara SeoBoost Settle Shouters Simple Catch SimpleClean Sketchly SNews SolidMag Someo Sophie Sora Sora Bank Sora Home Sora News Responsive Sora One Sora Ribbon Spido Sticko Stucco Sure Mag Swift Tech Tech Pro Techia Techmax TechSpot TemplateClue Templateism Templatesilk Templatesyard Templatezy The Freelancer TheBlogger TheFocus Themeforest Themexpose TIPS AND TRICKS Topside TravelMag Trendy Trendy Travel Tumblr Under Construction Uong Jowo Veethemes Vertox VideoPlay Vigo Waverly way2themes WebBiz Wedding X-Mag Zealer ZetaMag