简体   繁体   English

printf不起作用

[英]printf doesn't work

This C code is supposed to create some random numbers and print them and then sort them and print them again, but it just prints the sorted numbers. 该C代码应该创建一些随机数并打印它们,然后对其进行排序并再次打印,但它只是打印排序后的数字。 Could any body help me? 有人可以帮助我吗?

    #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]);
        } }

Your program is working correctly. 您的程序运行正常。 Try changing everything from 1000 to 10 just to test and see for yourself. 尝试将所有内容从1000更改为10,以进行测试并亲自观察。

What is happening is that it is printing everything out so quickly that the first 1000 is off the page. 发生的是,它是如此迅速地打印出所有内容,以至于第一个1000页面不在页面上。

The code is correct. 代码正确。

  1. Try a small size to array, 尝试小尺寸排列,
  2. write all the logs to a file. 将所有日志写入文件。

Code worked fine for me too. 代码对我来说也很好。 Maybe your terminal is not storing enough lines for you to see the beginning of the output. 也许您的终端没有存储足够的行供您查看输出的开始。 You can change that in the settings for your terminal. 您可以在终端的设置中进行更改。 Or you can cat them to a file instead. 或者,您也可以将它们放在文件中。 There is an easy option to do so if you google it. 如果您用google搜索,有一个简单的选择。 Also, add in another printf in between as a marker that is obvious like: 另外,在两者之间添加另一个printf作为标记,如下所示:

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

It will make it that much harder to miss it. 这将使它变得更加难以错过。 Good luck! 祝好运!

PS: to cat your output to a file simply type '> filename' when running the program. PS:要将您的输出显示为文件,只需在运行程序时键入'> filename'。 I called mine math.c so when I ran I typed: '$./math > file' And the whole output in in a file named 'file' 我调用了我的math.c,因此在运行时键入:'$。/ math> file',然后将整个输出存储在名为'file'的文件中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM