C语言 如何打印循环结果?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3576261/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 06:18:37  来源:igfitidea点击:

how to printf a loop result?

cfor-loopprintf

提问by esther

How to printf results from a loop?

如何从循环中打印结果?

For example if i have this something simple like this:

例如,如果我有这样简单的东西:

k[0]=2;
k[1]=3;
k[2]=4;

for (i = 0 ; i <= 2 ; i++)
{
    x[i]=5*k[i];
}

How do I print out the results for x[0],x[1],x[2] without having to keep repeating the array in printf? As in

如何打印出 x[0],x[1],x[2] 的结果而不必在 printf 中重复数组?如

printf("%d %d %d\n",x[0],x[1],x[2]);

I dont really want to do the printf above because for my problem i actally have an array of 100 values, i cant be repeating the x[0],x[1]... for one hundred times.

我真的不想做上面的 printf ,因为对于我的问题,我实际上有一个 100 个值的数组,我不能重复 x[0],x[1]... 一百次。

Hope someone can help out thanks loads!

希望有人能帮忙,谢谢!

回答by

Maybe this:

也许这个:

for (j = 0; j < 100; j++) {
    printf ("%d ",x[j]);
}
printf ("\n");

回答by Lou Franco

You can put a printf in a loop. If you don't put a "\n" in the printf, the next printf will be on the same line. Then when you are done with the loop, printf just a newline.

您可以将 printf 放入循环中。如果不在 printf 中放置“\n”,下一个 printf 将在同一行上。然后当你完成循环时, printf 只是一个换行符。

There is no printf type specifier for an array, you have to loop through the array elements and print them one by one.

数组没有 printf 类型说明符,您必须遍历数组元素并一一打印它们。

回答by locka

Why can't you print them out as you go around the loop?

为什么你不能在循环时将它们打印出来?

for (i = 0 ; i <= 2 ; i++)
{
    x[i]=5*k[i];
    printf("%d ", x[i]);
}
printf("\n");

Other than that you could have another loop underneath which strcats the results together into a single string for output.

除此之外,您还可以在下面有另一个循环,将结果合并到一个字符串中以供输出。

回答by Grimmy

You should place the printf statement in the loop:

您应该将 printf 语句放在循环中:

k[0]=2; 
k[1]=3; 
k[2]=4; 

for (i = 0 ; i <= 2 ; i++) 
{ 
    x[i]=5*k[i]; 
    printf("%d ", x[i]);
} 

printf("\n");

回答by Vibhakar SInha

You have stored 100 numbers in an array and want to display the same.

您在一个数组中存储了 100 个数字,并希望显示相同的数字。

In simple language, we can say an array is collection of items of similar types.

用简单的语言,我们可以说数组是相似类型项的集合。

For this, you have to first declare the array size, suppose you have declared an array as x[100], then it will store 100 numbers, with index number 0 to 99 or you can do the same by declaring each variable like x[0]=20,x[1]=30 .......x[99]=98, but this will be more time consuming.So its better if you do as follows.

为此,您必须首先声明数组大小,假设您已将数组声明为 x[100],那么它将存储 100 个数字,索引号为 0 到 99,或者您可以通过声明每个变量来执行相同的操作,例如 x[ 0]=20,x[1]=30 .......x[99]=98,但是这样会比较费时,所以按照下面的方法会更好。

Accept values

接受值

int x[100];    
 int index;
 for(index=0;index<100;index++)
 {
        printf("\nEnter %d number:",index+1);
        scanf("%d",x[i]);
  }

Display

展示

for(index=0;index<100;index++)

{

printf("\nArray element at index %d is %d",index,x[i];

}

This will be help you.

这会帮助你。

回答by Kangkan

You can think of doing it this way:

你可以考虑这样做:

k[0]=2;
k[1]=3;
k[2]=4;

for (i = 0 ; i <= 2 ; i++)
{
    x[i]=5*k[i];
    printf("%d",x[i]);
}
printf("\n");

回答by Andrei Ciobanu

#include <stdio.h>
#define FOR_PRINT(index, array, size, format) \
    { \
        for(index = 0; index < size; ++index){ \
            printf(format, array[index]); \
        } \
        printf("\n");
    } \

int main(){
    int array [] = {1,2,3};
    int i;
    FOR_PRINT(i, array, 3, "%d ");
    return (0);
}

You can always use an MACROfor that.

您始终可以为此使用

My previous example is not the safest, or the most readable example of a macro, but can be a starting point for you.

我之前的示例并不是最安全或最易读的宏示例,但可以作为您的起点。