热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

功能原型在主要内部声明-最佳实践?-Functionprototypedeclaredinsidemain-bestpractice?

Isthisagoodstyletohavethefunctionprototypedeclaredinsideofthemainfunction?这是在主函数内部声明

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 

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
  • 它可能会让一个新手感到困惑,认为该功能是由main主导的

  • in real code, I would normally put the function in a different compilation unit and #include its header file
  • 在实际代码中,我通常会将函数放在不同的编译单元中并#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点所提到的,你自己的头文件将被“”包围。


推荐阅读
  • Python正则表达式学习记录及常用方法
    本文记录了学习Python正则表达式的过程,介绍了re模块的常用方法re.search,并解释了rawstring的作用。正则表达式是一种方便检查字符串匹配模式的工具,通过本文的学习可以掌握Python中使用正则表达式的基本方法。 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • Linux重启网络命令实例及关机和重启示例教程
    本文介绍了Linux系统中重启网络命令的实例,以及使用不同方式关机和重启系统的示例教程。包括使用图形界面和控制台访问系统的方法,以及使用shutdown命令进行系统关机和重启的句法和用法。 ... [详细]
  • 展开全部下面的代码是创建一个立方体Thisexamplescreatesanddisplaysasimplebox.#Thefirstlineloadstheinit_disp ... [详细]
  • 本文介绍了游标的使用方法,并以一个水果供应商数据库为例进行了说明。首先创建了一个名为fruits的表,包含了水果的id、供应商id、名称和价格等字段。然后使用游标查询了水果的名称和价格,并将结果输出。最后对游标进行了关闭操作。通过本文可以了解到游标在数据库操作中的应用。 ... [详细]
  • CF:3D City Model(小思维)问题解析和代码实现
    本文通过解析CF:3D City Model问题,介绍了问题的背景和要求,并给出了相应的代码实现。该问题涉及到在一个矩形的网格上建造城市的情景,每个网格单元可以作为建筑的基础,建筑由多个立方体叠加而成。文章详细讲解了问题的解决思路,并给出了相应的代码实现供读者参考。 ... [详细]
  • 2018年人工智能大数据的爆发,学Java还是Python?
    本文介绍了2018年人工智能大数据的爆发以及学习Java和Python的相关知识。在人工智能和大数据时代,Java和Python这两门编程语言都很优秀且火爆。选择学习哪门语言要根据个人兴趣爱好来决定。Python是一门拥有简洁语法的高级编程语言,容易上手。其特色之一是强制使用空白符作为语句缩进,使得新手可以快速上手。目前,Python在人工智能领域有着广泛的应用。如果对Java、Python或大数据感兴趣,欢迎加入qq群458345782。 ... [详细]
  • vue使用
    关键词: ... [详细]
  • 本文介绍了设计师伊振华受邀参与沈阳市智慧城市运行管理中心项目的整体设计,并以数字赋能和创新驱动高质量发展的理念,建设了集成、智慧、高效的一体化城市综合管理平台,促进了城市的数字化转型。该中心被称为当代城市的智能心脏,为沈阳市的智慧城市建设做出了重要贡献。 ... [详细]
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • CSS3选择器的使用方法详解,提高Web开发效率和精准度
    本文详细介绍了CSS3新增的选择器方法,包括属性选择器的使用。通过CSS3选择器,可以提高Web开发的效率和精准度,使得查找元素更加方便和快捷。同时,本文还对属性选择器的各种用法进行了详细解释,并给出了相应的代码示例。通过学习本文,读者可以更好地掌握CSS3选择器的使用方法,提升自己的Web开发能力。 ... [详细]
  • Android Studio Bumblebee | 2021.1.1(大黄蜂版本使用介绍)
    本文介绍了Android Studio Bumblebee | 2021.1.1(大黄蜂版本)的使用方法和相关知识,包括Gradle的介绍、设备管理器的配置、无线调试、新版本问题等内容。同时还提供了更新版本的下载地址和启动页面截图。 ... [详细]
  • ZSI.generate.Wsdl2PythonError: unsupported local simpleType restriction ... [详细]
  • 推荐系统遇上深度学习(十七)详解推荐系统中的常用评测指标
    原创:石晓文小小挖掘机2018-06-18笔者是一个痴迷于挖掘数据中的价值的学习人,希望在平日的工作学习中,挖掘数据的价值, ... [详细]
  • http:my.oschina.netleejun2005blog136820刚看到群里又有同学在说HTTP协议下的Get请求参数长度是有大小限制的,最大不能超过XX ... [详细]
author-avatar
碎蜂CYM夜一
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有