return value of fgets()

S

Simon Biber

Roman said:
while ( fgets(buf, sizeof(buf), fp ) != (char*) 0 )

What's the special reason to compare result with 0 (casting to char pointer)
rather than simply with NULL?

There is no reason. This code is silly. Just use NULL.

Also, there is no need for parentheses around buf in `sizeof buf'. Using
parentheses makes one think that it is a type whose size is being
tested, but in this case it is just an object.
 
K

Keith Thompson

Roman Mashak said:
I met a source code where

while ( fgets(buf, sizeof(buf), fp ) != (char*) 0 )

What's the special reason to compare result with 0 (casting to char pointer)
rather than simply with NULL?

There's no good reason to use (char*)0 rather than NULL. On the other
hand, the effect is the same; NULL is just (IMHO) simpler and better
style.
 
J

Joe Wright

Roman said:
Hello, All!

I met a source code where

while ( fgets(buf, sizeof(buf), fp ) != (char*) 0 )

What's the special reason to compare result with 0 (casting to char pointer)
rather than simply with NULL?

With best regards, Roman Mashak. E-mail: (e-mail address removed)

Just bad taste. Assuming there is in scope 'char buf[N];'..

while (fgets(buf, sizeof buf, fp)) {}

...is the Wright way. The forms..

while (fgets(buf, sizeof buf, fp) != NULL) {}

while (fgets(buf, sizeof buf, fp) != 0) {}

...are acceptable of course.

Note that sizeof is an operator, not a function. It's argument is a
object's name or a type. If an object, no parentheses are necessary. If
a type, the Wright way is 'sizeof (long)' with a space before the '(' so
that it doesn't look so much like a function.
 
C

Chuck F.

Roman said:
I met a source code where

while ( fgets(buf, sizeof(buf), fp ) != (char*) 0 )

What's the special reason to compare result with 0 (casting to
char pointer) rather than simply with NULL?

None. It would be clearer with just NULL, or even 0. To my mind
there is no reason for a comparison at all (others will disagree).

while (fgets(buf, sizeof(buf), fp)) {
dostuff();
}
 
R

Roman Mashak

Hello, All!

I met a source code where

while ( fgets(buf, sizeof(buf), fp ) != (char*) 0 )

What's the special reason to compare result with 0 (casting to char pointer)
rather than simply with NULL?

With best regards, Roman Mashak. E-mail: (e-mail address removed)
 
K

Keith Thompson

Chuck F. said:
None. It would be clearer with just NULL, or even 0. To my mind
there is no reason for a comparison at all (others will disagree).

while (fgets(buf, sizeof(buf), fp)) {
dostuff();
}

Just to prove that you're right, I prefer an explicit comparison:

while (fgets(buf, sizeof(buf), fp) != NULL) {
dostuff();
}

But of course any C programmer needs to be able to read code using
either convention (and if you're maintaining existing code, you should
probably conform to the existing style unless it's horrendously bad).
 
P

pete

Keith said:
Just to prove that you're right, I prefer an explicit comparison:

while (fgets(buf, sizeof(buf), fp) != NULL) {
dostuff();
}

There's also:

while (fgets(buf, sizeof buf, fp) == buf) {
dostuff();
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,172
Messages
2,570,934
Members
47,474
Latest member
AntoniaDea

Latest Threads

Top