By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,305 Members | 1,614 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,305 IT Pros & Developers. It's quick & easy.

return value of fgets()

P: n/a
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: mr*@tusur.ru
Dec 16 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Roman Mashak wrote:
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.

--
Simon.
Dec 16 '05 #2

P: n/a
"Roman Mashak" <mr*@tusur.ru> writes:
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Dec 16 '05 #3

P: n/a
Roman Mashak wrote:
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: mr*@tusur.ru


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.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Dec 16 '05 #4

P: n/a
Roman Mashak wrote:

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();
}

--
A: Because it fouls the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Dec 16 '05 #5

P: n/a
"Chuck F. " <cb********@yahoo.com> writes:
Roman Mashak wrote:
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();
}


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).

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Dec 16 '05 #6

P: n/a
Keith Thompson wrote:

"Chuck F. " <cb********@yahoo.com> writes:
Roman Mashak wrote:
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();
}


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();
}

--
pete
Dec 16 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.