C编程语言(版本2)中的示例1

编程入门 行业动态 更新时间:2024-10-28 01:22:46
C编程语言(版本2)中的示例1-7(Example 1-7 from The C Programming Language(version 2))

我无法理解C语言中给出的示例1.7。 这个例子的主要目的是说明C中函数的使用。本书描述如下程序,“由于C没有像Fortran这样的指数运算符,所以我们通过编写一个函数来说明函数定义的机制功率(m,n)将整数m提升为正幂n,即幂(2,5)的值为32.这个函数不是一个实用的求幂例程,因为它只处理小整数的正幂,但对于插图来说已经足够了。“ 这是下面的C代码块:

#include <stdio.h> int power(int m, int n); /*function prototype */ int main() { /* test power function */ 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 the nth 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; }

我了解所有功能函数的代码块。 令我困惑的是循环。 我仍然在学C(显然,我在第一章),但我来自JavaScript。 所以当我看到这个循环时,我期望i需要被“绑定”到某个东西,以便它在我们的迭代中(类似于示例中的第一个for循环)。 但幂函数​​在p = p * base;后returns p p = p * base; 。 这与i没有任何关系。 对我来说,我认为,这个for循环的目的是什么? 我注意到,如果我注释掉for循环并删除整数i那么打印出来的数字除了之前for循环中的数字之外不会增加。 对我而言,我期望int n需要增加。 不是i 。

在我看来,在for循环中, n仅用于与i进行比较。 如果i <= n为真,power函数中for循环的目的仅仅是为了执行p = p * base ? 这不可能是真实的,因为如果那样的话,它最好只是一个if语句的服务器,并且不需要++i来递增。

我在这里错过了什么?

I am having trouble understanding the example 1.7 given in The C Programming Language. The main purpose of this example is to illustrate the use of functions in C. The book describes the following program as such, "Since C has no exponentiation operator like ** of Fortran, let us illustrate the mechanics of function definition by writing a function power(m,n) to raise an integer m to a positive power n. That is, the value of power(2,5) is 32. This function is not a practical exponentiation routine since it handles only positive powers of small integers, but it's good enough for illustration." This is the block of C code that follows:

#include <stdio.h> int power(int m, int n); /*function prototype */ int main() { /* test power function */ 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 the nth 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; }

I understand everything up to the power function's code block. What is confusing me is that for loop. I am still learning C(obviously, i'm in the first chapter) but I come from JavaScript. So when I see this for loop I expect the i to need to be 'bound' to something for it to be of us in iteration(similar to the first for loop in the example). But the power function returns p after p = p * base;. It's not returning anything to do with i. So to me, I think, what is the purpose of this for loop? I notice that if I comment out the for loop and remove the integer i then the numbers printed out do not increment except for the numbers within the previous for loop. To me, I expect the int n would need to increment. Not the i.

It appears to me that within the for loop that n is only being used as comparison to i. Is the purpose of the for loop in the power function only to execute p = p * base if i <= n is true? That can't be true because if that was then it would better be server with just an if statement and wouldn't need a ++i to increment.

What am I missing here?

最满意答案

power函数中的for循环反复地乘以(并更新) p 。 它做了n次 - i是柜台。 如果使用了if语句,那么您只能得到一个乘法。

The for-loop in the power function repeatedly multiplies (and updates) p by base. It does this n times -- i is the counter. If an if-statement were used, then you'd only get up to one multiplication.

更多推荐

本文发布于:2023-07-09 00:14:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1082116.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:示例   编程语言   版本

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!