今天在学习有关时间函数时,想让程序暂时挂起,一段时间后在继续执行!
用到了系统函数sleep();

在vc下sleep函数是以毫秒为单位,如果想让其停留3秒,需要这样做  sleep(3*1000);

需要头文件#include <windows.h>

#include<stdio.h>

#include<time.h>

#include<windows.h>

int main()

{

   time_t   timep;//定义 time_t类型变量timep

   struct tm *block;

   timep=time(NULL);//返回当前时间与1970年1月1日0时0分0秒相差的多少秒

   block=localtime(&timep );//把获取的时间储存

   printf("%s", asctime (  block) );

    //把本地时间转化为ASCII码打印出来

   Sleep(3000);

   timep=time(NULL);//返回当前时间与1970年1月1日0时0分0秒相差的多少秒

   block=localtime(&timep );//把获取的时间储存

   printf("%s", asctime (  block) );

   system("pause");

 return 0;

}

在把此函数移植在linux下发现好几个3秒都过去了,程序还是没反应。遂查了在vc和linux中sleep函数的区别。

发现在linux下sleep函数是以秒做单位的。

#include<stdio.h>

#include<time.h>

int main()

{

   time_t   timep;//定义 time_t类型变量timep

   struct tm *block;

   timep=time(NULL);//返回当前时间与1970年1月1日0时0分0秒相差的多少秒

   block=localtime(&timep );//把获取的时间储存

   printf("%s", asctime (  block) );

    //把本地时间转化为ASCII码打印出来

   Sleep(3);

   timep=time(NULL);//返回当前时间与1970年1月1日0时0分0秒相差的多少秒

   block=localtime(&timep );//把获取的时间储存

   printf("%s", asctime (  block) );

   system("pause");

 return 0;

}

在linux下每过一秒显示一下当前时间

#include<stdio.h>

#include<time.h>

int main()

{

   time_t   timep;//定义 time_t类型变量timep

   struct tm *block;

   int i;

   timep=time(NULL);//返回当前时间与1970年1月1日0时0分0秒相差的多少秒

   block=localtime(&timep );//把获取的时间储存

    printf("%s", asctime (  block) );

    //把本地时间转化为ASCII码打印出来

    

   for(i=0;i<3;i++)

    {

      sleep(1);

      printf("时间已过去%d秒\n",i+1);

      timep=time(NULL);//返回当前时间与1970年1月1日0时0分0秒相差的多少秒

      block=localtime(&timep );//把获取的时间储存

      printf("%s", asctime (  block) );

    }

   

 return 0;

}

在不同编译环境中如何使用sleep()函数的更多相关文章

  1. VS编译环境中TBB配置和C++中lambda表达式

    TBB(Thread Building Blocks),线程构建模块,是由Intel公司开发的并行编程开发工具,提供了对Windows,Linux和OSX平台的支持. TBB for Windows ...

  2. 鸿蒙内核源码分析(编译脚本篇) | 如何防编译环境中的牛皮癣 | 百篇博客分析OpenHarmony源码 | v58.01

    百篇博客系列篇.本篇为: v58.xx 鸿蒙内核源码分析(环境脚本篇) | 编译鸿蒙原来如此简单 | 51.c.h.o 本篇用两个脚本完成鸿蒙(L1)的编译环境安装/源码下载/编译过程,让编译,调试鸿 ...

  3. C语言编译环境中的 调试功能及常见错误提示

    文章目录 1 .调试功能 2 . 编译中的常见错误例析 3 .常见错误信息语句索引 1 .调试功能 1.常用健 <F10> : 激活系统菜单 <F6> : 将光标在编辑窗口和. ...

  4. 在C++11编译环境中,简单自测了一下C++标准库中的string/vector和迭代器,记录一下

    #include <iostream> #include <vector> using namespace std; int main() { //////////////// ...

  5. codeblocks出现'to_string' was not declared in this scope 的问题,用g++11编译环境

    在将数字转化为字符串时使用to_string()竟然出现了'to_string' was not declared in this scope,我头文件用的万能头文件肯定没问题,而这个函数在其他的CB ...

  6. Wabpack系列:在webpack+vue开发环境中使用echarts导致编译文件过大怎么办?

    现象,在一个webpack+vue的开发环境中,npm install echarts --save了echarts,然后在vue文件中直接使用 import echarts from 'echart ...

  7. 配置android source 在ubuntu中编译环境

    在Ubuntu中可以配置 android source 编译环境,推荐使用最新的64位的Ubuntu  LTS(Long Time Support); 1.安装JDK. AOSP主分支代码需要java ...

  8. Linux下LANMP集成环境中编译增加pdo_odbc模块

    linux版本为CentOs6.5,php集成环境为lanmp_v3.1,集成环境中默认的pdo扩展为:mysql, sqlite, sqlite2,现在有需求想链接微软的Access数据库,所以需要 ...

  9. MyEclipse中JDK运行环境和编译环境的设置

    一.设置myEclipse中新项目使用的JDK 1.运行环境   [Window]->[Preferences]->[Java]->[Installed JREs] 步骤:Add-- ...

随机推荐

  1. ssh登陆笔记📒

    ssh的配置 ssh的配置文件在/etc/ssh下,有两种配置文件,ssh_config和sshd_config. ssh_config是针对客户端的配置文件, sshd_config是针对服务端的配 ...

  2. java源代码如何打成jar包

    链接地址:http://jingyan.baidu.com/article/046a7b3ed8b23ef9c27fa9b9.html 有时自已写了一个很巧妙的方法,想分享给别人用,这时我们就可以将其 ...

  3. js中设置setInterval的注意点

    <html> <head> <meta http-equiv="Content-Type" content="text/html; char ...

  4. __call重载方法

    <?php class Person { function du() { echo "这是一个存在的方法"; } //该方法有两个参数,第一个参数 $function_nam ...

  5. C语言回溯算法解决N皇后问题

    回溯算法的模型是 x++, not satisfy ? x-- : continue. 代码中x作列号,y[x]保存第x列上皇后放置的位置. #include<stdio.h> #incl ...

  6. QT基本数据类型(以前没见过qintptr和qlonglong)

    QT的基本数据类型 qint8:signed char 有符号8比特数据 qint16:signed short 16位数据类型 qint32:signed int. 32位有符号数据类型 qint6 ...

  7. 用200行Python代码“换脸”

    介绍 本文将介绍如何编写一个只有200行的Python脚本,为两张肖像照上人物的“换脸”. 这个过程可分为四步: 检测面部标记. 旋转.缩放和转换第二张图像,使之与第一张图像相适应. 调整第二张图像的 ...

  8. GDB调试精粹及使用实例

    一:列文件清单 1. List (gdb) list line1,line2 二:执行程序 要想运行准备调试的程序,可使用run命令,在它后面可以跟随发给该程序的任何参数,包括标准输入和标准输出说明符 ...

  9. CUDA samples 2.3节 用CUDA示例来创建CUDA项目

    2.3.1. Creating CUDA Projects for Windows 略 2.3.2  Creating CUDA Projects for Linux 默认的samples的安装路径 ...

  10. uva11178 Morley’s Theorem(求三角形的角三分线围成三角形的点)

    Morley’s Theorem Input: Standard Input Output: Standard Output Morley’s theorem states that that the ...