What is the difference between global variables and local variables? Give an example to illustrate the same. (C++)
Asked by Arisha Nusrat
-
1 Answer
-
Hello Arisha,
Global variables can be accessed by any function within the class / package, where it is declared. Local variables, on the other hand, can be accessed only within a particular function where it has been declared.
E.g.: int a=10;
int disp()
{cout a; //displays value 10
int b=5;
cout b; //displays value 5}
cout a;//displays 10 as a is global variable
cout b; //Error. As b is a local variable defined in function disp(). Hence, cannot be accessed outside of the function.
I hope it helps. If so, kindly upvote this answer. If you have any further questions, kindly revert.
Related Questions
Taking an Exam? Selecting a College?
Get authentic answers from experts, students and alumni that you won't find anywhere else
Sign Up on ShikshaOn Shiksha, get access to
- 63k Colleges
- 963 Exams
- 610k Reviews
- -50000 Answers