C warning with char array: & ldquo; warning: array subscript has type 'char' & rdquo;


I am confused as to why this error occurs. My code execute fine and the results is exactly what I want but on compiling my c file with the command: "gcc -std=c99 -Wall -o assignment2 assignment2.c"

Here is the code:

Here is the output error:

I would just use this but i'm sure it might have some reasons why an error shows and also because my assignment marker probably want accept it if there is this warning on compilation.

If you are using i for a counter, use int instead of char

int i = 0;

this will fix the problem.

But also, c should be an int, because fgetc() can return EOF which is an int, and in general the return type of fgetc() is int, thus you should use int for c too.

Your code should be then

char word[500];
int i = 0;
int c;

while (((c = fgetc(fp)) != EOF) && (c != '\n')) {
    word[i++] = (char) c;
word[i] = '\0';