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.

Recommended Answers

All 2 Replies

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

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!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.