搜索
查看: 7391|回复: 9
打印 上一主题 下一主题

总有新同学问 Sleep(1000); 和system("pause");和getchar();的区别

[复制链接]
跳转到指定楼层
楼主
发表于 2014-3-8 13:48:19 | 显示全部楼层 回帖奖励 |倒序浏览 |阅读模式
Sleep(3000); 是等待3秒。(需要再顶部添加#include <windows.h>
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <windows.h>
  4. int main()
  5. {
  6.     printf("Cyuyan! ");
  7.     Sleep(3000);
  8.     return 0;
  9. }
复制代码


system("pause");就是暂停直到你输入任意一个字符结束
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int main()
  4. {
  5.     printf("Cyuyan! ");
  6.     system("pause");
  7.     return 0;
  8. }
复制代码



getchar();getchar();
本质上等待读入两个字符,也可以起到暂停的作用,但是别人本质上不是用来暂停程序的。这里做了一个假象,等待读入字符。
那为什么一个getchar();不行呢。其实也不是都不行。如果程序没有屏幕输入的话,一个getchar();也可以。但是往往程序都有输入。而我们在输入的时候通常需要敲一个回车来接受输入,第一个getchar();就把回车个读掉了,因此需要第二getchar();来假装还需要读入一个字符,来起到暂停的效果。
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int main()
  4. {
  5.     printf("Cyuyan! ");
  6.     getchar();getchar();
  7.     return 0;
  8. }
复制代码







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

本版积分规则

广播台
特别关注
快速回复 返回顶部 返回列表