0

Nested functions(function declarations in block scope) are not allowed in C standards(ANSI[C89], C99, C11).

But I couldn't find stating it in C standards.

Edit :

Why a function definition cannot be in a function definition(compound statement)?

ylem
  • 157
  • 2
  • 6
  • 1
    The standard exists to state what constitutes a valid C program, not to state the millions of things that don't – StoryTeller - Unslander Monica Apr 24 '17 at 09:23
  • 1
    A standard notmally says what you _can_ do and not what you _can not_ do. Since there is no statement in the standard saying you can define nested functions, you cannot. – Paul Ogilvie Apr 24 '17 at 09:24
  • @PaulOgilvie 6.2.1.4 states that If the declarator or type specifier that declares the identifier appears inside a block or within the list of parameter declarations in a function definition, the identifier has block scope, which terminates at the end of the associated block. And 6.7.5.3 states function declarators. So according to it, function declarators can be block scope? – ylem Apr 24 '17 at 09:39
  • 1
    A [declaration is not definition](http://stackoverflow.com/questions/1410563/what-is-the-difference-between-a-definition-and-a-declaration). – Ilja Everilä Apr 24 '17 at 09:40
  • I find the downvote unjustified.This question may be asked due to a misunderstanding of the c standard, but that's why we are here. – jwsc Apr 24 '17 at 12:31

3 Answers3

5

There is a difference between a function declaration and a function definition. A declaration merely declares the existence of a function and a definition defines the function.

int f(void) { /* ... */ } // function definition
int f(void);              // function declaration

In 6.9.1 the syntax for a function is defined as

function-definition: declaration-specifiers declarator declaration-listopt compound-statment

In 6.8.2, the things you can put in a compound statement are defined as a declaration or a statement. A function definition isn't considered to be either of these syntactically.

So yes, a function declaration is legal in a function but a function definition is not e.g.

int main(int argc, char*argv[])
{
    int f(void);                // legal
    int g(void) { return 1; } ; // ILLEGAL

    // blah blah
}
JeremyP
  • 84,577
  • 15
  • 123
  • 161
2

It may not be stated directly but if you work through the grammar for function-definition you'll find they're not accepted within the grammar.

Why? According to Dennis Richie (who is a bit of an authority on the matter) it appears they were excluded from the start:

"Procedures can be nested in BCPL, but may not refer to non-static objects defined in containing procedures. B and C avoid this restriction by imposing a more severe one: no nested procedures at all."

https://www.bell-labs.com/usr/dmr/www/chist.html

It's humor to avoid a restriction by imposing a more severe one. I read this as it being a simplifying maneuver. Nested procedures add complexity to the compiler (which Ritchie was very keen to limit on machines of the time) and add little value.

The standardization process was (wisely) never seen as an opportunity to extend C willy-nilly and (from the same document):

"From the beginning, the X3J11 committee took a cautious, conservative view of language extensions."

It's difficult to put a case that nested functions offer significant benefits so it's not surprising that even if some implementations were supporting them they weren't adopted as standard.

In general the standards efforts ever since have been at least equally conservative and again it's difficult to see a lot of support amongst implementers to add such a feature.

At the end of the day if you're worried that some function be used outside its intended purpose and is (logically) a sub-function of exactly one given function then give it static linkage and introduce another source file or even whole translation unit.

Persixty
  • 8,165
  • 2
  • 13
  • 35
-1

Nested or private functions are something that many C compilers used to allow, but are not part of the C standard, and now it's quite rare to find compilers that support them, certainly by default.

The standard is determined by a committee, and nested functions will be something that they have discussed, and there will be a rationale, but I don't know what it is offhand, nor do most C programmers. Nested functions aren't inherently a bad idea, but you can achieve virtually all of the benefits by writing a static file scope function, which is the method of creating private functions which was standardised.

Malcolm McLean
  • 6,258
  • 1
  • 17
  • 18
  • I would not call gcc a "rare compiler". – too honest for this site Apr 24 '17 at 09:55
  • 1
    I would not call gcc a strictly standard-compliant compiler (by default) either, since it uses a ton of extensions (nested functions being one of them). –  Apr 24 '17 at 09:57
  • @InternetAussie extensions comply with the standard, if they don't break any conforming program – M.M Apr 24 '17 at 09:58
  • @InternetAussie: Non sequitur! The answer already states that nested functions are an extension. By definition extensions make a compiler not strictily compliant, because they make otherwise invalid constructs valid. Using `-std=c11` gcc is as compliant as any other compiler, if not more. So what's your point? – too honest for this site Apr 24 '17 at 09:59
  • A static function does not have access to the local vars declared in main(). – ThingyWotsit Apr 24 '17 at 15:24