4 ms·
Nested functions are a GCC extension, not standard C. As far as I know, there's never been a reason to use the auto keyword in standard C.
by AdamH12113 3y ago
Nested functions are a GCC extension, not standard C. As far as I know, there's never been a reason to use the auto keyword in standard C.
- tpoacher 3y agoInteresting, I hadn't realised... So in standard c, all function prototypes and definitions must always appear at toplevel?
- kps 3y agoDefinitions, yes. Declarations can appear inside functions, but it's not common to see. int f(int x) { extern int g(int); return g(x + 1) - 1; }
- tpoacher 3y agoThis is doubly interesting, since the whole point of auto in the nested scenario is precisely to prevent the default "externness" of the declaration. But, if the definition itself cannot appear in the enclosing block scope in the first place without the gcc extension, then I suppose an auto keyword here would still be meaningless (if not misleading / dangerous possibly UB?).
- uecker 3y agoauto is not allowed for a function declaration with block scope without the GCC extension.
- AdamH12113 3y agoThat is correct, yes. You can limit the scope of a function to a single file using the static keyword; otherwise all functions are in the same global namespace for static linking. In practice, this is not a big limitation by itself. Although C has function pointers, it's not a functional programming language -- there are no closures, and you can't create new functions at run-time under any circumstances. (Not within the language itself, anyway. You'd need to embed a compiler, or at least a minimal code generator.) On Harvard-architecture systems like microcontrollers, code and data may be in different memories (ROM vs. RAM) with different access permissions, and some platforms even put them in entirely separate address spaces.