I am trying to run the following code:
#include <sys/time.h> #include <stdio.h> int main() { unsigned int ms, oldms = 0,dif; struct timeval tv; while(1) { gettimeofday(&tv, NULL); ms=tv.tv_sec; //printf("%d\n",ms-oldms ); dif=ms-oldms; if(dif>3) { printf("3 seconds up"); oldms=ms; } } }
I expect it to print โ3 seconds upโ every 3 seconds, but it does not display this message. I tried debugging it with gdb but nothing seems to be wrong and still doesn't output. While trying to debug, I added the printf statement, and the magic output can be seen.
If I run the program after deleting // printf ("% d \ n", ms-oldms); expression, there is no way out. I am not sure what is happening and whether it depends on anything.
$ gcc --version gcc (Ubuntu 4.8.2-19ubuntu1) 4.8.2
source share