They don't need to be global - they just need to be the same variable. Local variables in a function are "recreated" every time the function is called.
You have local variables that are static, which means that the variable remains each time.
There are also, usually, many other ways to have local but persistent storage - the typical way is to pass something from a "never ending" function to the lower level function, which is some sort of struct that contains your actual "global" variables.
--
Mats