如何在C编程中表达K=1:N
,即在1
到N
之间?
我是C的新手。
#include <math.h>
#include <stdio.h>
int main(){
int N=length(x);
float K=1:N;
printf("%f", K);
};
答案 0 :(得分:1)
可能最强大的方法是使用整数索引并在每次迭代时将其转换为循环内的float:
int j;
for (j = 1 ; j <= N; ++j) // iterate integer j from 1 to N
{
float K = (float)j; // convert integer j to float K
...
}
请注意,这比使用float
作为循环变量更安全,因为它不容易出现舍入错误,这可能会导致测试循环终止条件时出现问题。
答案 1 :(得分:0)
您可以将K声明为整数
int k;
然后使用任何循环 while或for循环
for-Loop:
for(k=1;k<=N;K++)
{
printf("%d ",k);
//if you want to print or use in float use casting printf("%f ",(float) k);
}
<强> while循环:强>
k=1;
while(k<=N)
{
printf("%d ",k);
//if you want to print or use in float use casting printf("%f ",(float) k);
k++;
}