1

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:

enter image description here

Here is the output error:

enter image description here

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.

1
  • 2
    Why is i a char? Commented Mar 16, 2017 at 2:10

2 Answers 2

5

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';
Sign up to request clarification or add additional context in comments.

3 Comments

@KeineLust, the code had Undefined Behavior, thanks for the edit.
Oh man, I was trying this and that from other answers online and ended up missing this little mistake. Thanks a lot for pointing that out for me.
@jjyj That's what Stack Overflow is for, and it's fun to help. But keep in mind, that you should post actual code instead of images next time.
-2

i should be evaluated so that i < 500 is fullfilled

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.