本文介绍了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\n",usage.ru_stime);
    printf("%ld.%6ld\n",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:

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\n", 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编程)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 05:05