K&放第1.8章;的R - 想不通为什么行" INT功率(INT男,INT N);"包括 [英] Section 1.8 of K & R - can't figure out why line "int power(int m, int n);" is included

查看:152
本文介绍了K&放第1.8章;的R - 想不通为什么行" INT功率(INT男,INT N);"包括的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过K&放工作; R以学习编程。进展顺利,到目前为止,但我不清楚线code从1.8节(函数)的作用。

I'm working through K & R to learn programming. Going well so far, but I'm unclear about the role of a line of code from section 1.8 (functions).

在1.8节中,作者向您展示如何创建一个函数来一个整型提升到另一个整数的力量。

In section 1.8, the authors show you how to create a function to raise one integer to the power of another integer.

我已经粘贴了以下code,因为它是写在这本书。一切都输出罚款。但我不知道为什么他们已经包括这条线在顶部:

I've pasted the code below, as it was written in the book. Everything outputs fine. But I don't know why they've included this line at the top:

int power(int m, int n);

这本书并没有提到它,除了他说,该计划将提高整米的n次方。如果我删除从code线,程序仍输出,因为它应该。

The book doesn't mention it, apart from saying that the program will raise integer m to the power n. If I remove the line from the code, the program still outputs as it should.

如果我正确地理解这一点,行

If I understand this correctly, the line

int power(int base, int n)

创建的功能,并在大括号下方定义该函数。然后在主括号调用函数输出图表。

creates the function, and the braces underneath define the function. Then the braces under main call the function to output the chart.

因此​​,所有这似乎是有道理的。但我没有看到最上面一行做了什么。

So all that seems to make sense. But I don't see what the very top line does.

这可能是多余的,但似乎更有可能的是,我失去了一些东西。任何人都可以开导我,为什么该行是吗?

It could be extraneous, but it seems far more likely that I'm missing something. Can anyone enlighten me as to why that line is there?

谢谢!

(我的第一篇,顺便说一句,这个网站是一个很好的资源)

(my first post, by the way. This site is a great resource)

#include <stdio.h>

int power(int m, int n);

/* test power function */
main()
{
int i;

    for (i = 0; i < 10; ++i)
        printf("%d %d %d\n", i, power(2,i), power(-3, i));
    return 0;
}

/* power: raise base to n-th power; n >= 0 */

int power(int base, int n)
{
    int i, p;

    p = 1;
    for (i = 1; i <= n; ++i)
        p = p * base;
    return p;
}

*的修改的*我很IM $ P $在答复的速度和实用性pssed。我贴这个,去了一个快速步行,回来后在25分钟内找到回答我的问题多个职位。谢谢大家。

*Edit*I'm very impressed at the speed and usefulness of the replies. I posted this, went for a quick walk, and came back in 25 minutes to find multiple posts that answered my question. Thanks everyone.

推荐答案

第一行是函数的的声明的。 code在底部的块是的定义的功能的。

The first line is the declaration of the function. The block of code at the bottom is the definition of the function.

在1999年版的ISO C标准的开始,这是非法的(违反约束)来调用一个函数没有可见的声明;声明必须precede呼叫。

Starting with the 1999 version of the ISO C standard, it's illegal (a constraint violation) to call a function without a visible declaration; the declaration must precede the call.

对于这样一个简单的程序,你可以只写的完整定义功率()的定义主要(之前(自定义还提供了一个声明),但是对于更复杂的情况下(如递归调用),你常常需要提供一个单独的声明。

For a simple program like this one, you could just write the full definition of power() before the definition of main() (since a definition also provides a declaration), but for more complex cases (such as recursive calls) you often need to provide a separate declaration.

对于大型程序,这是常见的收集所有的功能的声明的在头文件( foo.h中,例如),以及在源文件中相应的定义( foo.c的,例如)。 A 的#includefoo.h中指令是用来做的声明在其他文件中可见。你会看到,这种事情在本书后面。

For larger programs, it's common to collect all the function declarations in a header file (foo.h, for example), and the corresponding definitions in a source file (foo.c, for example). A #include "foo.h" directive is used to make the declarations visible in other files. You'll see that kind of thing later in the book.

(在1990年及更早版本C,这就是K&放大器; R2覆盖,在有些情况下,你可以调用一个函数没有可见的声明的情况下 - 但它仍然是一个非常不错的主意提供明确的声明反正。)

(In the 1990 and earlier versions of C, which is what K&R2 covers, there are cases where you can call a function without a visible declaration -- but it's still a very good idea to provide explicit declarations anyway.)

顺便说一下,主要程序的声明确实应该 INT主要(无效)而不仅仅是的main()

Incidentally, the declaration of the main program should really be int main(void) rather than just main().

术语:一个原型是一个函数声明,指定类型的参数。

Terminology: a "prototype" is a function declaration that specifies the types of the parameters.

int power(int base, int n);    /* a declaration that's also a prototype */
int power(int, int);           /* likewise */
int power();                   /* a declaration but not a prototype */

(参数名称需要一个定义,但可选择在一个独立的声明。)

(Parameter names are required in a definition, but optional in a standalone declaration.)

作为一个特殊的情况下,原型的函数不带参数的用途(无效),因为空括号已经意味着非原型声明。因此, INT主要(无效)是一个雏形,但 INT的main()不是

As a special case, a prototype for a function with no parameters uses (void), since empty parentheses already mean a non-prototype declaration. So int main(void) is a prototype, but int main() isn't.

非原型声明是过时,这意味着它们在理论上可以从一个未来语言标准被移除。但他们已经过时自1989年以来,甚至在新的2011 ISO C标准的委员会还没有看到适合将其删除。

Non-prototype declarations are "obsolescent", meaning that they could in theory be removed from a future language standard. But they've been obsolescent since 1989, and even in the new 2011 ISO C standard the committee hasn't seen fit to remove them.

这篇关于K&放第1.8章;的R - 想不通为什么行&QUOT; INT功率(INT男,INT N);&QUOT;包括的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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