Function inside function in C

Someone please consider this error: -

void main() { int a=5, b=60, func(); printf("\nI am in main-1"); int func(){ printf("\nI am in funct"); return 1; } func(); printf("\nI am in main-2"); } 

I get the following errors:

  • In the function "main":
  • Line 8: error: static declaration 'func' follows unsteady declaration
  • Line 4: Error: The previous 'func' declaration was here
  • Line 3: warning: return type 'main' not 'int'

I think C allows a nested class because the following code works fine:

 void outerfunc() { int func() { printf("\nI am in funct"); return 1; } func(); } void main() { printf("\nI am in main-1"); outerfunc(); printf("\nI am in main-2"); } 
+6
source share
8 answers

You are using the GNU C compiler extension, which allows you to declare nested functions. The error occurs because front declarations of nested functions under the GCC extension must be added with the auto keyword.

 int a=20,b=11; int main() { int a=5, b=60; auto int func(); // <--------- here func(); // <- call it printf("\nI am in main-1"); int func(){ printf("\nI am in funct"); return 1; } printf("\nI am in main-2"); return 0; } 

See http://gcc.gnu.org/onlinedocs/gcc/Nested-Functions.html for more details.

+12
source

ANSI C does not allow the definition of a nested function. And your main function should return int .

+9
source

Nested functions are not allowed in standard C / C ++. Just (forward) declare func() inside main() if you want to define it later.

 int main() { int a=5, b=60, func(); printf("\nI am in main-1"); int func(); // <---- declare inside main() printf("\nI am in main-2"); } int func(){ // <---- define later printf("\nI am in funct"); return 1; } 
+1
source

What you do is a specific GCC function , it has never been a β€œproper” C function (i.e., part of the ANSI C specification).

If you want to use this function, I believe that you are following this:

 #include <stdio.h> int a = 20, b = 11; int main( int argc, char* argv[] ) { int a = 5, b = 60; auto int func( void ); printf("\nI am in main-1"); int func( void ) { printf("\nI am in funct"); return 1; } printf("\nI am in main-2"); return func(); } 

The reason your previous code didn't work is because the nested functions are not bound:

A nested function always has no binding. Declaring one with an external or static error. If you need to declare a nested function before defining it, use auto (which is otherwise pointless for function declarations).

The above example uses the auto keyword. I also took the liberty of fixing your ad main

+1
source

Nested functions are a gcc-specific extension; they are not universally supported.

Regarding the warning about main , the standard signatures for main are

 int main(void) int main(int argc, char **argv) // or equivalent 

An implementation may provide additional signatures (some compilers allow a third parameter for environment variables), but these additional signatures must be documented during implementation; IOW, void main() is only a valid signature for main if your compiler documentation explicitly lists it as such.

If in doubt, use one of the standard signatures above.

+1
source
  • You did not define func before calling it.
  • Refers to the source string.
  • You do not return int.
0
source

It works if you remove the func() declaration in int declarations.

0
source

C ++ does not allow the inclusion of functions inside other functions.

Attempting to do this in VS 2010 yields:

'funct': local function definitions are illegal

You need to move this function and declare it outside the main one.

0
source

Source: https://habr.com/ru/post/896707/


All Articles