I have some code as following:
#include <stdio.h>
#include <stdlib.h>
static counter = 0;
static flag = 0;
int main()
{
int number = 3;
int i = 0;
for(i; i< number; i++)
{
if(counter >= number)
{
counter = 0;
flag = 1;
}
counter ++;
printf(" counter = %u\n", counter);
printf(" flag = %u\n", flag);
}
return 0;
}
everytime when the counter reach 3, the flag should be 1, but why when counter reach 3, the flag is still 0?
You loop runs only 3 times. In the last iteration, during the
iftest, value ofcounteris 2 and value ofnumberis 3. And the if test fails. Now counter is incremented to 3 which is what is printed.