[英]Find out output of c-program
#include<stdio.h>
void compute(int);
int cube(int);
int main( )
{
compute(3);
}
void compute(int in)
{
int res=0,i;
for(i=1;i<=in;i++);
{
res=cube(i);
printf("%d %d",res,i);
}
}
int cube(int n)
{
return (n*n*n);
}
~ 〜
output : 64 4 输出:64 4
How does it happen ? 它是如何发生的?
分号在您for
行的结尾。
由于您的for
行上使用了分号,因此该语句将i递增,直到它不小于等于3的3,然后运行。
Since you are using C99, you may want to get into the habit of declaring the variable controlling a for loop inside the for statement itself. 由于您使用的是C99,因此您可能想养成在for语句本身内部声明控制for循环的变量的习惯。
for (int i=1; i<=in; i++);
{
/* i is not in scope here */
}
And, now that I've written that, the idiomatic way to write a loop is to start at 0
and test with <
而且,既然我已经写了,编写循环的惯用方式是从0
开始并用<
for (int i=0; i<in; i++);
{
/* i is not in scope here */
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.