c - 轮询 C 中的时钟周期数 sw

标签 c time clock

我正在尝试连续轮询时钟周期数并仅在周期计数为 100 + 开始时间时才打印我的语句。这样如果我的 start = 1000000,打印语句应该只在 end = 1000100 时执行 它应该显示例如假设在开始时,时间 = 1000000 您好,开始值:1000000 结束值:1000101

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void main()
{
        clock_t start,end;
        start = clock();
        while(end=clock() < (start + 100))
        {};
        printf("Hello, value of start:%d and value of end:%d", start, end);
}

但是我越来越 您好,start的值:0和end的值:0

我已经编辑了我的代码并获得了 end=0.0000 的值。这是我在下面编辑的代码

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void main()
{
        clock_t start;
        start = clock();
        clock_t end = start;
        printf("Hello");
        while(end < (start + 100))
        {
                end = clock();
        };
        printf("value of end is %f \n", end);
}

最佳答案

我很确定这一行:

while(end=clock()< start + 100)

需要是这样的:

while( (end=clock()) < (start + 100) )

写得更好:

void main()
{
        clock_t start = clock();
        clock_t end = start;
        while(end < (start + 100))
        {
            end = clock();
        };
        printf("Hello, value of start:%d and value of end:%d", (int)start, (int)end);
}

关于c - 轮询 C 中的时钟周期数 sw,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39823284/

相关文章:

c - 我在 C 中的扫雷程序代码出现段错误

在这里找不到错误... C 代码

android - 如何让数字时钟只显示小时和分钟

Java 对数时钟给出不准确的答案

c - 如何在C中打印用户输入的数字的位数?

使用 sscanf 将字符串转换为时间

c++ - clock_gettime() 每 50-100 毫秒返回大约 1-2 毫秒的误差(Virtualbox 上的 Debian wheezy)

PHP - 将 12 小时格式时间转换为新时区时出现错误

swift - 使用计时器更新时间时,如何使时间与macOS系统时钟同步?

c - 打印错误 "address pointing at code space is taken"