鱼C论坛

 找回密码
 立即注册
查看: 1417|回复: 2

为什么不先打印printf?

[复制链接]
发表于 2015-5-11 04:18:21 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. #include <stdio.h>
  2. #include <time.h>//这个头文件定义了一秒等于:CLOCKS_PER_SEC
  3. #define MAX 5
  4. //clock()函数能计算进程启动的时间:tick
  5. //当tick数递增到与CLOCKS_PER_SEC时, 就表示是一秒
  6. int main(int argc, char *argv[])
  7. {
  8.         clock_t nowtime = 0;
  9.         printf("---这程序将在5秒后退出---");
  10.         nowtime = clock();//记录下启动时时间
  11.         while((clock() - nowtime) != (CLOCKS_PER_SEC*MAX)) continue;
  12.         return 0;
  13. }
复制代码

为什么都是一打印就运行完了?一打开时, 没反应, 一打印是, 退出了, 为什么printf不先打印再计算时差?

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2015-5-11 06:04:45 | 显示全部楼层
  1. #include <stdio.h>
  2. #include <stdlib.h> //for rand(), srand()
  3. #include <time.h> //for clock(), rand(), srand()
  4. #define LIMIT 10
  5. #define NUMBER_LEN 5
  6. int main(int argc, char **argv)
  7. {
  8.         int count = 0, input = 0;
  9.         time_t  nowtime = 0;
  10.         //clock_t nowtime = 0;
  11.         srand(time(NULL));//发现这个放在rand()开头就行, 不用在每次调用rand()时再调用一次
  12.         printf("测试:按ctrl+c退出\n");
  13.         while(1)
  14.         {
  15.                 for(count = 0; count < NUMBER_LEN; count++ )//数字长度
  16.                         printf("%d ", rand()%LIMIT);//输出数字

  17.                 //这里停止一秒
  18.                 nowtime = clock();//记录下启动时时间
  19.                 while((clock() - nowtime) < CLOCKS_PER_SEC) continue;

  20.                 printf("\r");//把光标放到数数字最前面
  21.                 for(count = 0; count < NUMBER_LEN; count++)
  22.                         printf("  ");//打印一串空格把字符盖掉
  23.                 printf("\r");

  24.                 //这里停止一秒
  25.                 nowtime = clock();//记录下启动时时间
  26.                 while((clock() - nowtime) < CLOCKS_PER_SEC) continue;
  27.         }
  28.         return 0;
  29. }
  30. //程序在linux下无法通过, 很奇怪在win下正常运行,
  31. //看标准定义, clock()函数应是一样的才对
  32. //难道linux下的clock()与win下的不一样?!
复制代码


小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-5-11 06:47:35 | 显示全部楼层
为什么一开如没打印, 而是结束时才打印? 测试系统为linux
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-19 22:39

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表