- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Difference Between Local and Global Variable
In this post, we will understand the difference between local and global variables.
Local variable
It is generally declared inside a function.
If it isn’t initialized, a garbage value is stored inside it.
It is created when the function begins its execution.
It is lost when the function is terminated.
Data sharing is not possible since the local variable/data can be accessed by a single function.
Parameters need to be passed to local variables so that they can access the value in the function.
It is stored on a stack, unless mentioned otherwise.
They can be accessed using statement inside the function where they are declared.
When the changes are made to local variable in a function, the changes are not reflected in the other function.
Local variables can be accessed with the help of statements, inside a function in which they are declared.
Example
Following is an example −
#include <stdio.h> int main () { /* local variable declaration */ int a, b; int c; /* actual initialization */ a = 10; b = 20; c = a + b; printf ("value of a = %d, b = %d and c = %d
", a, b, c); return 0; }
Global variable
It is declared outside the function.
If it isn’t initialized, the value of zero is stored in it as default.
It is created before the global execution of the program.
It is lost when the program terminates.
Data sharing is possible since multiple functions can access the global variable.
They are visible throughout the program, hence passing parameters is not required.
It can be accessed using any statement within the program.
It is stored on a specific location inside the program, which is decided by the compiler.
When changes are made to the global variable in one function, these changes are reflected in the other parts of the program as well.
Example
Following is an example −
#include /* global variable declaration */ int g; int main () { /* local variable declaration */ int a, b; /* actual initialization */ a = 10; b = 20; g = a + b; printf ("value of a = %d, b = %d and g = %d
", a, b, g); return 0; }
- Related Articles
- Difference between static, auto, global and local variable in C++
- What is the difference between global and local variables in Python?
- What is the difference between global and local Variables in JavaScript?
- What is the difference between Local Events and Global Events in jQuery?
- Global and Local Variables in Python?
- Global and Local Variables in Java
- Global and Local Variables in C#
- Global and Local Inversions in C++
- Difference Between Identifier and Variable
- Difference between datetime and datetime-local in HTML5
- Global vs Local variables in Python
- What are local variables and global variables in C++?
- Difference between Session Storage and Local Storage in HTML5
- How are C++ Local and Global variables initialized by default?
- Difference between Local Storage, Session Storage, and Cookies in JavaScript
