Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

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.


Interesting, I hadn't realised...

So in standard c, all function prototypes and definitions must always appear at toplevel?


That 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.


Definitions, 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;
    }


This 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?).


auto is not allowed for a function declaration with block scope without the GCC extension.




Consider applying for YC's Summer 2026 batch! Applications are open till May 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: