UNIX 编程.struct timeval 如何打印它(C 编程) [英] UNIX Programming. struct timeval how to print it (C-programming)

查看:37
本文介绍了UNIX 编程.struct timeval 如何打印它(C 编程)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试打印 timeval 类型的值.实际上我可以打印它,但我收到以下警告:

I am trying to print a value of type timeval. Actually I am able to print it, but I get the following warning:

此行有多个标记

  • 格式%ld"需要类型long int",但参数 2 的类型为struct timeval"

程序编译并打印值,但我想知道我是否做错了什么.谢谢.

The program compiles and it prints the values, but I would like to know if I am doing something wrong. Thanks.

    printf("%ld.%6ld
",usage.ru_stime);
    printf("%ld.%6ld
",usage.ru_utime);

使用类型

typedef struct{
    struct timeval ru_utime; /* user time used */
    struct timeval ru_stime; /* system time used */
    long   ru_maxrss;        /* maximum resident set size */
    long   ru_ixrss;         /* integral shared memory size */
    long   ru_idrss;         /* integral unshared data size */
    long   ru_isrss;         /* integral unshared stack size */
    long   ru_minflt;        /* page reclaims */
    long   ru_majflt;        /* page faults */
    long   ru_nswap;         /* swaps */
    long   ru_inblock;       /* block input operations */
    long   ru_oublock;       /* block output operations */
    long   ru_msgsnd;        /* messages sent */
    long   ru_msgrcv;        /* messages received */
    long   ru_nsignals;      /* signals received */
    long   ru_nvcsw;         /* voluntary context switches */
    long   ru_nivcsw;        /* involuntary context switches */
}rusage;

struct rusage usage;

推荐答案

在 GNU C 库中struct timeval:

在 sys/time.h 中声明并具有以下成员:

is declared in sys/time.h and has the following members:

long int tv_sec

这表示经过的时间的整秒数.

This represents the number of whole seconds of elapsed time.

long int tv_usec

这是剩余的经过时间(几分之一秒),以微秒数表示.总是少于一百万.

This is the rest of the elapsed time (a fraction of a second), represented as the number of microseconds. It is always less than one million.

所以你需要这样做

printf("%ld.%06ld
", usage.ru_stime.tv_sec, usage.ru_stime.tv_usec);

获得格式良好"的时间戳,例如 1.000123.

to get a "nicely formatted" timestamp like 1.000123.

这篇关于UNIX 编程.struct timeval 如何打印它(C 编程)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆