gpt4 book ai didi

timer - UNIX 编程。 struct timeval 如何打印它(C 编程)

转载 作者:行者123 更新时间:2023-12-03 02:54:59 27 4
gpt4 key购买 nike

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

该行有多个标记

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

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

    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;

最佳答案

In the GNU C Library , struct timeval:

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\n", usage.ru_stime.tv_sec, usage.ru_stime.tv_usec);

获得“格式良好”的时间戳,例如1.000123

关于timer - UNIX 编程。 struct timeval 如何打印它(C 编程),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1469495/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com