Quantcast

A community for students. Sign up today!

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

devendra_iiti

  • 2 years ago

How does a compiler knows that a variable is global or local........

  • This Question is Open
  1. andrew.m.higgs
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    A variable declared outside of a function is considered global within the function. A variable declared within a function is considered local within the function. global_name = 'fred' global_surname = 'smith' def func_of_sorts(): local_surname = 'bloggs' global global_surname, global_name print global_name, global_surname print global_name, local_surname func_of_sorts() # call your function print global_name, global_surname print global_name, local_surname I can only use global_name and global_surname in my function because of line 'global global_surname, global_name'. The result of the above will be: fred smith fred bloggs fred smith fred Traceback (most recent call last): File "/home/andy/Ubuntu One/ELearning/MOOC/Week 4/global_variable_test.py", line 13, in <module> print global_name, local_surname NameError: name 'local_surname' is not defined The reason is because the compiler does not have local_surname defined in the main block, only in the function. Hope this helps.

  2. devendra_iiti
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    I want to ask that what happens inside the compiler(in c or c++)...... How will compiler trace that given variable is global or local(its scope)

  3. andrew.m.higgs
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    I am no compiler expert. :-) I would assume that it is due to scopes yes. Regards

  4. manishdablu
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    bro.. what do you think of compiler? compiler works as a parser which converts the source code into object code. so compiler will know about given variable from its declaration. and if you thinking what happens at execution time then it's defferent things

  5. devendra_iiti
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    yes, I want to know what happens at execution time........

  6. manishdablu
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    first of all execution of a program is not the work of compiler. the compiled code can be executed without the help of compiler. the compiled prog is linked with appropriate lib and loaded in the appropriate place in memory. so local and global variables are located at different places in the main memory (RAM). local variable is generally present in stack of the process.

  7. devendra_iiti
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    thanks

  8. Not the answer you are looking for?
    Search for more explanations.

    • Attachments:

Ask your own question

Ask a Question
Find more explanations on OpenStudy

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.