0

I wrote a small program in C on Ubuntu having function definitions within the main function and compiled it using GCC. It compiled and ran without error. However, writing the same program in Codeblocks on Windows doesn't compile, and issues an error that says function-definition is not allowed here.

So, does GCC on Ubuntu allow function definitions within a function? Will it always run perfectly (given that it has compiled and there is no logical error in the program)?

Clifford
  • 88,407
  • 13
  • 85
  • 165
Anubhab Das
  • 101
  • 4
  • 1
    That's a gcc extension, not part of standard C. Don't use it if you want portable code. – Shawn Oct 20 '19 at 11:09
  • GCC is a compiler Code::Blocks is not - it is an IDE for use with a number of compilers - which are you using? Most commonly Code::Blocks on Windows uses MinGW/GCC (i.e. GCC). Whether the extension to enable nested functions is enabled is a matter of what arguments you pass to the compiler (primarily `--ansi` and `--std=` see https://gcc.gnu.org/onlinedocs/gcc/C-Dialect-Options.html#C-Dialect-Options – Clifford Oct 20 '19 at 11:24
  • 1
    Also https://gcc.gnu.org/onlinedocs/gcc/Nested-Functions.html and https://stackoverflow.com/questions/2929281/are-nested-functions-a-bad-thing-in-gcc. Be aware however, that the GCC version you are using between Ubuntu and Windows may differ, and you should ideally consult the documentation for the respective versions: https://gcc.gnu.org/onlinedocs/ – Clifford Oct 20 '19 at 11:25
  • @Shawn Okay, but even my code::blocks uses GCC. So, the same code should work? – Anubhab Das Oct 20 '19 at 11:39
  • Make sure you are compiliing as C not C++. Nested functions work only in gcc and only when compiliing C. – Piotr Praszmo Oct 20 '19 at 11:47
  • @PiotrPraszmo Okay, I was compiling it as a .cpp file. Compiling it as a C file works. Thanks! – Anubhab Das Oct 20 '19 at 11:54

0 Answers0