+ 1

What is the difference between a static local variable and a global variable?

confuse when to use them.

6th Jul 2018, 6:25 PM
DerpyCoco
DerpyCoco - avatar
2 Respostas
+ 6
Hello, DerpyCoco ! Local variable: These variables exist only within the specific function that creates them. They are unknown to other functions and the main program. Thus, they are usually implemented using a stack. Local variables cease to exist after the completion of the function that created them. They are recreated every time a function is executed or called. Global variable: These variables can be accessed (ie known) by any function that contains the program. They are implemented by associating memory locations with variable names. They are not recreated if the function is called.
6th Jul 2018, 6:30 PM
Alexander Sokolov
Alexander Sokolov - avatar
+ 1
Those are two absolute different things. Global variable is a variable, defined in a namespace, outside any function and class. Global variables can be accessed and modified by any function. In general using global variables are not recommended, because they make your code unstable and bug prone. And static local variable are visible and accessible only from the function, in which they are defined. But, unlikely to regular variables, they are not destroyed after the function completes and they can be accessed on the next call of the function. For example: int counter() { static int counter=0; return counter++; } This function will return the number of times, it was called.
6th Jul 2018, 9:02 PM
Дмитро Іванов
Дмитро Іванов - avatar