Make a function that reports how many times it has been called, using a static local.
#include <stdio.h>
void greet(const char *name) {
static int calls = 0; /* survives between calls */
calls++;
printf("Hello, %s! (call %d)\n", name, calls);
}
int main(void) {
greet("Asha");
greet("Ravi");
greet("Meera");
return 0;
}
Hello, Asha! (call 1) Hello, Ravi! (call 2) Hello, Meera! (call 3)
The counter is private to greet() but outlives each call. A global would also work, but any other function could then change it.