Is declaring global variables a good practice????

Pensu
@pensu-8tNeGU โ€ข Oct 27, 2024

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

Welcome, guest

Join CrazyEngineers to reply, ask questions, and participate in conversations.

CrazyEngineers powered by Jatra Community Platform

  • Manashree Thokal

    @manashree-7mZH3V • Aug 8, 2011

    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-rOaCSy • Aug 18, 2011

    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.