0

Hi everyone, i have this portion of code

while(c = getc(file) != EOF)

c is an int and file is a FILE *file = fopen(filename, "r");

The filename points to a file with 2 lines, 10 characters total. That while always makes c a value of 1 (accodring to ASCII -> start text, dunno what that mean).
But when i use this while:

while((c = getc(file)) != EOF)

It works perfect. I though that was equivalent...i'm missing something on basic syntax here. What's wrong in the first while?

Thank you.

2
Contributors
2
Replies
3
Views
8 Years
Discussion Span
Last Post by neithan
0

Hi everyone, i have this portion of code

while(c = getc(file) != EOF)

c is an int and file is a FILE *file = fopen(filename, "r");

The filename points to a file with 2 lines, 10 characters total. That while always makes c a value of 1 (accodring to ASCII -> start text, dunno what that mean).
But when i use this while:

while((c = getc(file)) != EOF)

It works perfect. I though that was equivalent...i'm missing something on basic syntax here. What's wrong in the first while?

Thank you.

Try investigating order of operations

0

Try investigating order of operations

Ok! I looked it up, != is processed before = . So, when != is processed (without the parenthesis)... So c gets the result of the logical question getc(file) != EOF !! That's why it was all ones...!

Thank you!

Edited by neithan: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.