C程序检测时间变化并重置计数器

C program to detect the time change and reset the counter

我的应用程序中有一个内置算法。每当我 运行 算法时,它都会在数据库中保存准确的 运行 时间。例如,如果我在 11.00 运行 算法,它会节省那个时间。同样,如果我在 11.05 运行,它会在数据库中保存 11.05。

我要检测它的次数运行。所以一旦它 运行s 5 次,我需要做一些动作,比如改变值并将计数器重置为 0。所以当它达到 5 次迭代时,我应该再次重置计数器。

我是初学者。如果您能帮助我了解语法,那将会很有帮助。

MAIN
{


 int temp1, temp2, flag = 0, max = 5;

 temp1 = GET_INT_VALUE(8,1,84,1,0);


 if flag = 0;
 while(1)
 {
     if (templ == temp2)
         flag++;
     else
         flag = 0;

     if (flag == max)
     {
        //sprintf(Message,"SE value is %d",temp2);             
        PRINTOUT("Message");
        flag = 0;
        break;
     }

 }
}
END

最好的方法是使用静态局部变量,如下所示:

void foo(void) {
    static int counter = 0;

    counter++;
    if(counter > 5) {
        counter = 0;

        /* Do something every fifth time */
    }
}

请注意,您不能使用普通局部变量(例如int counter = 0;),因为它的内容在函数returns 时会丢失。 static 使它更像一个全局变量(因此它的值在函数 returns 时不会丢失)。