N
N
npcprom2017-09-19 12:51:22
C++ / C#
npcprom, 2017-09-19 12:51:22

Why do local functions work in C?

I accidentally discovered that in C you can declare local functions (a function within a function) as for example in Pascal. The forums write that this is not supported in C and CPP.
Compiler gcc 6.3.

#include <stdlib.h>
#include <stdio.h>

void Foo()
{
  void Too()
  {
    printf( "Too\n" );
  }
  
  printf("Foo\n");
  Too();	
}

int main( void )
{
  printf( "Main\n" );
  Foo();
  return 0;
}

stdout:
Main
Foo
Too

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mercury13, 2017-09-19
@Mercury13

Obviously a GNU extension.
MinGW, even with -std=c99, OK.
And it only works in C mode. MinGW/C++
C:\TestApps\NestedFunction3\main.cpp|7|error: a function-definition is not allowed here before '{' token|
Embarcadero x86 (Borland):
[bcc32 Error] File1.c(7): E2141 Declaration syntax error
Embarcadero x64 (clang):
[bcc64 Error] File1.c(6): expected ';' end of declaration

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question