Is declaring global variables a good practice????

Hey ppl,

This is a good question asked to my friend in his interview. They asked him to make a simple stack program. He made it using array and declaring it globally so each function could access it easily. But they told him, its not good to use global variables in program. And according to him this might be the reason he didnt get selected. Can anyone throw some more light on this????

Replies

  • Manashree Thokal
    Manashree Thokal
    Global variables, though convenient to access should not much put into practice. This very advantage can as well be a disadvantage. As it can be accessed by any function, there could be conflicting values as they are modified. It better with local variables where there scope is limited, hence can be managed efficiently.
  • vik001ind
    vik001ind
    There is no such disadvantage of using global variables unless you know where to use local & global variables. Global variables are stored in data segment whereas local variables are stored in stack. Creating global variable saves you from passing varibles & saving stack memory. Also creatind too many local variables are not recommended.

You are reading an archived discussion.

Related Posts

Hi guys, I hope to be ok, i am a student in computer engineering subject. i am looking for some subjects in which we need to do a search on...
Don’t be so SWEET that people will gulp you, Don’t be so BITTER that people will hate you. Sail on the ship of SUCCESS And land on the port of...
Today Our CEan- MegaByte is celebrating her Birthday, Wishing you many many Happy Returns of the day Megabyte. 🎉 :myparty:
which of these project has more application...robotic arm controlled via>> accelerometer or by voice ??i have resources for both but confused wich is more application oriented ?(level of difficulty is...
hiii friends my name is Gowtham pursuing my final year btech in cse and as per our academic curriculum I have to complete a research paper in any area. My...