How should we call a function just once from a function which is called 'n' no. of times from the main function without using static variable? [closed]

I would pass parameter instead of static int to be reentrant.
then caller can decide to reinit the state when desired.

int main()
{
    int state = 0;

    abc(&state);
    abc(&state);
    abc(&state);
    abc(&state);
    return 0;
}

void abc(int* state)
{
    if (*state == 0)
    {
        xyz();
        ++*state;
    }
    printf("This is abc");
}

Browse More Popular Posts

Leave a Comment