So I need to write a function which counts the frequency of a character in a string in C. The function parameters must be just the character I want to count. So here's my code:
#include <stdio.h>
#include <string.h>
int charFrequency(char c) {
    char str[500];
    int i, nr = 0;
    printf("Enter the string: ");
    fgets(str, sizeof(str), stdin);
    for(i=0; i < strlen(str); ++i) {
        if (str[i] == c) {
            nr++;
        }
    }
    return nr;
}
int main(void) {
    char c;
    int nr;
    printf("Enter the character you want to count: ");
    c = getc(stdin);
    getc(stdin); // what is the role of this code line? if I delete this line the programs does not work anymore.
    nr = charFrequency(c);
    printf("The frequency is %d", nr);
    return 0;
}
I have a problem when I read the character I want to count. I use getc but the program doesn't work fine. But if I write again getc(stdin) the program works fine.
I also tried with scanf but I have the same issue. Can someone explain me how getc works?
 
    