Scope of variables

what is the difference between global variables and local static variables? when they should be used?

@zodiac hey shantanu singh if there is so many function and you have to use a common value again and again so we declare variable in global variable and you have to use some specific value for a function so you can take local variable this is example.

1 Like

And what is the difference between global and static local?

Static variables have a property of preserving their value even after they are out of their scope!Hence, static variables preserve their previous value in their previous scope and are not initialized again in the new scope.
A static int variable remains in memory while the program is running. A normal or auto variable is destroyed when a function call where the variable was declared is over.

For example, we can use static int to count number of times a function is called, but an auto variable can’t be used for this purpose.

Hey Shantanu,
As you are not responding to this thread, I am marking your doubt as Resolved for now. Re-open it if required.

Please mark your doubts as resolved in your course’s “ Ask Doubt ” section, when your doubt is resolved.