printf 不起作用

printf doesn't work

这段 C 代码应该创建一些随机数并打印它们,然后对它们进行排序并再次打印它们,但它只打印排序后的数字。任何人都可以帮助我吗?

    #include <stdio.h>
    #include <time.h>
    #include <stdlib.h>
    int main(){
        int i, j, k;
        float temper;
        time_t t;
        float grades[1000];
        fflush(stdout);
        printf("Here are the number\n");
        srand(time(&t));
        for(i=0;i<1000;i++){
            grades[i]=rand();
            printf("%f\n", grades[i]);
        }
        for(i=0;i<1000;i++){
            int swap=0;
            for(j=i;j<1000;j++){
                if(grades[i]>grades[j]){
                    temper=grades[i];
                    grades[i]=grades[j];
                    grades[j]=temper;
                    swap=1;
                }
            }
        }
        printf("sorting is done");
        for(i=0;i<1000;i++){
            printf("%f\n", grades[i]);
        } }

您的程序运行正常。尝试将所有内容从 1000 更改为 10,以便自己测试和查看。

发生的事情是它打印所有内容的速度如此之快,以至于前 1000 个不在页面上。

代码正确。

  1. 尝试小尺寸排列,
  2. 将所有日志写入文件。

代码对我来说也很好用。也许您的终端没有存储足够的行让您看到输出的开头。您可以在终端的设置中更改它。或者你可以将它们 cat 到一个文件中。如果您 google 有一个简单的选择。此外,在两者之间添加另一个 printf 作为标记,如:

printf("+++++++++++++++++++++++++++ here is the break point ++++++++++++++++");

这会让错过它变得更加困难。祝你好运!

PS: 要将输出输出到文件,只需在 运行 程序时键入 '> 文件名'。我叫我的 math.c 所以当我 运行 我输入: '$./math > 文件' 整个输出在一个名为 'file'

的文件中