最好的做法 - 主要内声明的函数原型? [英] Function prototype declared inside main - best practice?

查看:108
本文介绍了最好的做法 - 主要内声明的函数原型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个良好的作风有main函数内声明的函数原型?

我一直在寻找一个C教程,我觉得是相当过时。然而,他们宣称的主要内部函数原型。我通常在main外面声明

 的#include<&stdio.h中GT;诠释的main()
{
    烧焦MYNAME [30];
    INT theage;
    INT getage();    的printf(\\ n输入您的姓名:);
    得到(MYNAME);
    theage = getage();
    的printf(\\ n年龄=%d个和NAME =%S,theage,MYNAME);
    返回0;
}INT getage()
{
    INT myage; / *本地只getage()* /    的printf(\\ n输入您的年龄:);
    scanf函数(%d个,&安培; myage);
    返回(myage);
}


解决方案

我个人会说不有以下几个原因:


  • 这让code主更长

  • 这可能会混淆一个新手进入思考ING功能由主
  • 范围
  • 在实际code,我通常把功能不同的编译单元和#包括其头文件

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.

#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);
}

解决方案

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

这篇关于最好的做法 - 主要内声明的函数原型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆