Is this a good style to have the function prototype declared inside of the main function?
这是在主函数内部声明函数原型的好方法吗?
I was looking at a C tutorial, I think is quite out of date. However, they declare the function prototype inside of main. I normally declare outside before main.
我正在看一个C教程,我认为已经过时了。但是,他们在main中声明了函数原型。我通常在主要之前宣布在外面。
#include <stdio.h>
int main ()
{
char myname [30];
int theage;
int getage ();
printf ("\nEnter your name:");
gets (myname);
theage = getage ();
printf("\n AGE = %d and NAME = %s", theage, myname);
return 0;
}
int getage ()
{
int myage; /* local to only getage() */
printf ("\nEnter your age: ");
scanf ("%d",&myage);
return (myage);
}
5 个解决方案
#1
16
I personally would say "no" for several reasons:
我个人会说“不”,原因如下:
- it makes the code for main longer
- it may confuse a newbie into think ing the function is scoped by main
- in real code, I would normally put the function in a different compilation unit and #include its header file
它使主要代码更长
它可能会让一个新手感到困惑,认为该功能是由main主导的
在实际代码中,我通常会将函数放在不同的编译单元中并#include其头文件
#2
5
I'll also say no with the additional reason that if you start using explicit declarations all over the code, you will most definitely get unresolved externals when the function you are calling suddenly changes its signature. If you have ONE declaration in ONE header file, you only need to change ONE declaration when the function changes.
我还会说,如果您开始在整个代码中使用显式声明,那么当您调用的函数突然改变其签名时,您肯定会得到未解析的外部因素。如果在一个头文件中有一个声明,则只需在函数更改时更改一个声明。
However, I'd say yes because of the following reason: If you are just writing a simple test method that's written for a single use only, i.e. if you want to test something really quick and then discard the function right away. Then it can be nifty to just throw in a declaration right before you want to make the call.
但是,由于以下原因,我会说是的:如果您只是编写一个仅为一次性使用而编写的简单测试方法,即如果您想要快速测试某些内容然后立即丢弃该功能。然后,在您想要拨打电话之前,只需输入一个声明就可以了。
For production code -> No no no ! :)
对于生产代码 - >不不不! :)
#3
4
It's not a good style.
这不是一个好的风格。
Either declare the local function prototypes at the beginning or move them to a header-file.
要么在开始时声明本地函数原型,要么将它们移动到头文件。
Function protoypes (and external variables as well) can be declared almost everywhere in the c-language. However, just because it's possible shouldn't be no reason to write spaghetti style C.
函数原型(以及外部变量)几乎可以在c语言的任何地方声明。然而,仅仅因为它可能不应该没有理由写意大利面风格C.
It makes the code less readable. For me such practices are a clear sign of code-smell.
它使代码的可读性降低。对我来说,这种做法是代码嗅觉的明显标志。
#4
1
i think that's just a small example for the tutorial... this is what you do when you start to introduce functions...
我认为这只是本教程的一个小例子...这就是你开始介绍函数时所做的事情......
I agree with Neil...
我同意尼尔......
#5
1
Since I haven't jumped through the required number of hoops in this pony show I have no choice but to post this comment as an answer.
由于我没有在这个小马表演中跳过所需数量的篮球,我别无选择,只能发表这个评论作为答案。
Keep in mind that this is just a snippet from a book and not the kind of code that one sees in a production environment. The code snippet is fine but not ideal. Neil gave the best answer so I gave him +1. I would note his 3rd point if you really want to know how it's done outside of tutorial/text books.
请记住,这只是书中的一个片段,而不是人们在生产环境中看到的那种代码。代码片段很好但不理想。尼尔给出了最好的答案,所以我给了他+1。如果你真的想知道它是如何在教程/教科书之外完成的,我会注意到他的第三点。
Also, a point since I'm making them: the "stdio.h" vs is simply a way of telling the preprocessor where to search for the file stdio.h. Again, in most situations you will see stdio.h surrounded by <> instead of "". However, your own header files, as mentioned by Neil's 3rd point, will be surrounded by "".
此外,我正在制作它们:“stdio.h”vs只是告诉预处理器在哪里搜索文件stdio.h的一种方式。同样,在大多数情况下,您会看到stdio.h被<>而不是“”包围。但是,正如Neil的第3点所提到的,你自己的头文件将被“”包围。
#1
16
I personally would say "no" for several reasons:
我个人会说“不”,原因如下:
- it makes the code for main longer
- it may confuse a newbie into think ing the function is scoped by main
- in real code, I would normally put the function in a different compilation unit and #include its header file
它使主要代码更长
它可能会让一个新手感到困惑,认为该功能是由main主导的
在实际代码中,我通常会将函数放在不同的编译单元中并#include其头文件
#2
5
I'll also say no with the additional reason that if you start using explicit declarations all over the code, you will most definitely get unresolved externals when the function you are calling suddenly changes its signature. If you have ONE declaration in ONE header file, you only need to change ONE declaration when the function changes.
我还会说,如果您开始在整个代码中使用显式声明,那么当您调用的函数突然改变其签名时,您肯定会得到未解析的外部因素。如果在一个头文件中有一个声明,则只需在函数更改时更改一个声明。
However, I'd say yes because of the following reason: If you are just writing a simple test method that's written for a single use only, i.e. if you want to test something really quick and then discard the function right away. Then it can be nifty to just throw in a declaration right before you want to make the call.
但是,由于以下原因,我会说是的:如果您只是编写一个仅为一次性使用而编写的简单测试方法,即如果您想要快速测试某些内容然后立即丢弃该功能。然后,在您想要拨打电话之前,只需输入一个声明就可以了。
For production code -> No no no ! :)
对于生产代码 - >不不不! :)
#3
4
It's not a good style.
这不是一个好的风格。
Either declare the local function prototypes at the beginning or move them to a header-file.
要么在开始时声明本地函数原型,要么将它们移动到头文件。
Function protoypes (and external variables as well) can be declared almost everywhere in the c-language. However, just because it's possible shouldn't be no reason to write spaghetti style C.
函数原型(以及外部变量)几乎可以在c语言的任何地方声明。然而,仅仅因为它可能不应该没有理由写意大利面风格C.
It makes the code less readable. For me such practices are a clear sign of code-smell.
它使代码的可读性降低。对我来说,这种做法是代码嗅觉的明显标志。
#4
1
i think that's just a small example for the tutorial... this is what you do when you start to introduce functions...
我认为这只是本教程的一个小例子...这就是你开始介绍函数时所做的事情......
I agree with Neil...
我同意尼尔......
#5
1
Since I haven't jumped through the required number of hoops in this pony show I have no choice but to post this comment as an answer.
由于我没有在这个小马表演中跳过所需数量的篮球,我别无选择,只能发表这个评论作为答案。
Keep in mind that this is just a snippet from a book and not the kind of code that one sees in a production environment. The code snippet is fine but not ideal. Neil gave the best answer so I gave him +1. I would note his 3rd point if you really want to know how it's done outside of tutorial/text books.
请记住,这只是书中的一个片段,而不是人们在生产环境中看到的那种代码。代码片段很好但不理想。尼尔给出了最好的答案,所以我给了他+1。如果你真的想知道它是如何在教程/教科书之外完成的,我会注意到他的第三点。
Also, a point since I'm making them: the "stdio.h" vs is simply a way of telling the preprocessor where to search for the file stdio.h. Again, in most situations you will see stdio.h surrounded by <> instead of "". However, your own header files, as mentioned by Neil's 3rd point, will be surrounded by "".
此外,我正在制作它们:“stdio.h”vs只是告诉预处理器在哪里搜索文件stdio.h的一种方式。同样,在大多数情况下,您会看到stdio.h被<>而不是“”包围。但是,正如Neil的第3点所提到的,你自己的头文件将被“”包围。