Tom St Denis said:
so you make
int test(void)
{
int a, b;
int test2(void) { return a + b; }
a = 4; b = 5;
return test2() + 3;
}
can be turned into
---
static int a, b;
static int test2(void) { return a + b; }
int test(void) { a = 4; b = 5; return test2() + 3; }
---
That's not the same thing at all. In the nested-function version
(which is not valid C), a and b are local variables, so each
invocation of test() gets its own unique copy of each of them. In
your static version, the lifetime of a and b is the entire execution
of the program; recursive calls to test() will share the same copies.
If your goal is to create a function that does the same thing as the
original one, you might as well write
int test(void)
{
return 12;
}
If you want to write something that duplicates the semantics of nested
functions with access to the parent's local variables, it's possible
(though probably not worth the effort), but making the local variables
static isn't the way to do it.