热门标签 | 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点所提到的,你自己的头文件将被“”包围。


推荐阅读
  • 深入理解Redis的数据结构与对象系统
    本文详细探讨了Redis中的数据结构和对象系统的实现,包括字符串、列表、集合、哈希表和有序集合等五种核心对象类型,以及它们所使用的底层数据结构。通过分析源码和相关文献,帮助读者更好地理解Redis的设计原理。 ... [详细]
  • 前言--页数多了以后需要指定到某一页(只做了功能,样式没有细调)html ... [详细]
  • 本文详细介绍如何利用已搭建的LAMP(Linux、Apache、MySQL、PHP)环境,快速创建一个基于WordPress的内容管理系统(CMS)。WordPress是一款流行的开源博客平台,适用于个人或小型团队使用。 ... [详细]
  • Python处理Word文档的高效技巧
    本文详细介绍了如何使用Python处理Word文档,涵盖从基础操作到高级功能的各种技巧。我们将探讨如何生成文档、定义样式、提取表格数据以及处理超链接和图片等内容。 ... [详细]
  • 深入解析Android自定义View面试题
    本文探讨了Android Launcher开发中自定义View的重要性,并通过一道经典的面试题,帮助开发者更好地理解自定义View的实现细节。文章不仅涵盖了基础知识,还提供了实际操作建议。 ... [详细]
  • 导航栏样式练习:项目实例解析
    本文详细介绍了如何创建一个具有动态效果的导航栏,包括HTML、CSS和JavaScript代码的实现,并附有详细的说明和效果图。 ... [详细]
  • 本文基于刘洪波老师的《英文词根词缀精讲》,深入探讨了多个重要词根词缀的起源及其相关词汇,帮助读者更好地理解和记忆英语单词。 ... [详细]
  • 本文详细介绍了Akka中的BackoffSupervisor机制,探讨其在处理持久化失败和Actor重启时的应用。通过具体示例,展示了如何配置和使用BackoffSupervisor以实现更细粒度的异常处理。 ... [详细]
  • UNP 第9章:主机名与地址转换
    本章探讨了用于在主机名和数值地址之间进行转换的函数,如gethostbyname和gethostbyaddr。此外,还介绍了getservbyname和getservbyport函数,用于在服务器名和端口号之间进行转换。 ... [详细]
  • 本文深入探讨了HTTP请求和响应对象的使用,详细介绍了如何通过响应对象向客户端发送数据、处理中文乱码问题以及常见的HTTP状态码。此外,还涵盖了文件下载、请求重定向、请求转发等高级功能。 ... [详细]
  • 本文详细探讨了HTML表单中GET和POST请求的区别,包括它们的工作原理、数据传输方式、安全性及适用场景。同时,通过实例展示了如何在Servlet中处理这两种请求。 ... [详细]
  • 本文介绍如何使用Objective-C结合dispatch库进行并发编程,以提高素数计数任务的效率。通过对比纯C代码与引入并发机制后的代码,展示dispatch库的强大功能。 ... [详细]
  • 本教程涵盖OpenGL基础操作及直线光栅化技术,包括点的绘制、简单图形绘制、直线绘制以及DDA和中点画线算法。通过逐步实践,帮助读者掌握OpenGL的基本使用方法。 ... [详细]
  • 根据最新发布的《互联网人才趋势报告》,尽管大量IT从业者已转向Python开发,但随着人工智能和大数据领域的迅猛发展,仍存在巨大的人才缺口。本文将详细介绍如何使用Python编写一个简单的爬虫程序,并提供完整的代码示例。 ... [详细]
  • 本文详细介绍了如何在ECharts中使用线性渐变色,通过echarts.graphic.LinearGradient方法实现。文章不仅提供了完整的代码示例,还解释了各个参数的具体含义及其应用场景。 ... [详细]
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社区 版权所有