in C I am writing some of my very first exercises. Earlier on, I tried to declare a simple function inside of main and it comes with an error: "function definition is not allowed here". But I thought a function could be declared inside of main or outside, the only difference being the scope?? I have also read, in here, of other people writing functions inside of main, so why won't it let me do it? thanks
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
You can declare a function inside another function:
int main(void) {
int foo(int); // declaration
...
}
But you can't define a function inside another function:
int main(void) {
// Doesn't work.
int foo(int x) {
return x * 2;
}
...
}
Also, declaring functions inside other functions is a really unusual thing to do, and essentially never necessary.