Counting Num. of Function Executions
Let's say I use a qsort() function to "sort" a randomly generated integer array. To sort the array I use the compare_int() function as shown below, but I also want to count how many times I call the compare_int() function, how would I go about doing this without using any global variables?
Code:
#include <stdio.h>
int compare_int(int* x, int* y);
int count = 0;
int main(void)
{
int rand_array[35];
int i = 0;
srand(time(0));
printf("Unsorted: \n");
for (i=0; i < 35; i++)
{
rand_array[i] = rand() % 666;
printf("%d ",rand_array[i]);
}
printf("\n\n");
qsort(rand_array,35,sizeof(int),compare_int);
printf("Sorted: \n");
for (i=0; i < 35; i++)
printf("%d ",rand_array[i]);
printf("\n%d comparisons used\n",count);
return 0;
}
int compare_int(int* x, int* y)
{
count++;
if (*x > *y)
return 1;
else if (*x < *y)
return -1;
else
return 0;
}
That method uses a global variable count, and I simply just increment count every time I execute the compare_int() function. How do I go about doing the same thing without using the global var count? I've tried using extern/static modifiers to no luck, any hints would be greatly appreciated :)