What is the difference between global variables and local variables? Give an example to illustrate the same. (C++)

0 57 Views | Posted 7 years ago
Asked by Arisha Nusrat

  • 1 Answer

  • Shiksha Ask & Answer

    Answered by

    Rishi Broto Chakraborty | Guide-Level 15

    7 years ago
    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.

Get authentic answers from experts, students and alumni that you won't find anywhere else

Sign Up on Shiksha

On Shiksha, get access to

  • 63k Colleges
  • 963 Exams
  • 610k Reviews
  • -50000 Answers

Share Your College Life Experience

Didn't find the answer you were looking for?

Search from Shiksha's 1 lakh+ Topics

or

Ask Current Students, Alumni & our Experts

×
×

This website uses Cookies and related technologies for the site to function correctly and securely, improve & personalise your browsing experience, analyse traffic, and support our marketing efforts and serve the Core Purpose. By continuing to browse the site, you agree to Privacy Policy and Cookie Policy.

Need guidance on career and education? Ask our experts

Characters 0/140

The Answer must contain atleast 20 characters.

Add more details

Characters 0/300

The Answer must contain atleast 20 characters.

Keep it short & simple. Type complete word. Avoid abusive language. Next

Your Question

Edit

Add relevant tags to get quick responses. Cancel Post