473,785 Members | 2,309 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Recommended style

Hi all,

Which is the recommended style in production code for the following -

....
fp = fopen("xyz.txt" , "r");
if (fp == NULL) {
....
}

OR
fp = fopen("xyz.txt" , "r");
if (!fp) {
....
}

Thanks.

Nov 14 '05
62 2609

"Bonj" <a@b.com> wrote in message news:33******** *****@individua l.net...
Neither:
fp = fopen("xyz.txt" , "r");
if (fp == NULL) {
...
}
is nearly right, but that's java style. Since your question is titled
'Recommended style', the correct style is
fp = fopen("xyz.txt" , "r");
if (fp == NULL)
{
...
}

is correct, but if ... is only one line, then I prefer just
fp = fopen("xyz.txt" , "r")
if(fp == NULL)
...

or better still
if(fopen("xyz.t xt", "r") == NULL) ...
And now how will you refer to the stream?



They both will work, but if(!fp) is less explicit,
IMO it's quite explicit. A test for true/false (zero/nonzero).
as it's not actually a
boolean value you're testing, it's a returned handle, and you're testing
whether it's NULL or not.


You're testing whether 'fp', after being converted to 'int',
is zero or not.

-Mike
Nov 14 '05 #41
"Mike Wahler" <mk******@mkwah ler.net> writes:
"Bonj" <a@b.com> wrote in message news:33******** *****@individua l.net...

[...]
or better still
if(fopen("xyz.t xt", "r") == NULL) ...


And now how will you refer to the stream?



They both will work, but if(!fp) is less explicit,


IMO it's quite explicit. A test for true/false (zero/nonzero).
as it's not actually a
boolean value you're testing, it's a returned handle, and you're testing
whether it's NULL or not.


You're testing whether 'fp', after being converted to 'int',
is zero or not.


There's no conversion to int. The unary '!' operator tests whether
its operand compares equal to 0. For a pointer, comparison to 0 is
comparison to a null pointer constant.

--
Keith Thompson (The_Other_Keit h) 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.
Nov 14 '05 #42

"Keith Thompson" <ks***@mib.or g> wrote in message
news:ln******** ****@nuthaus.mi b.org...
"Mike Wahler" <mk******@mkwah ler.net> writes:
"Bonj" <a@b.com> wrote in message news:33******** *****@individua l.net...

[...]
or better still
if(fopen("xyz.t xt", "r") == NULL) ...


And now how will you refer to the stream?



They both will work, but if(!fp) is less explicit,


IMO it's quite explicit. A test for true/false (zero/nonzero).
as it's not actually a
boolean value you're testing, it's a returned handle, and you're testing whether it's NULL or not.


You're testing whether 'fp', after being converted to 'int',
is zero or not.


There's no conversion to int. The unary '!' operator tests whether
its operand compares equal to 0. For a pointer, comparison to 0 is
comparison to a null pointer constant.


But what is the type of the expression !fp?

I was thinking in the wider context of 'if's (full)
conditional expression itself, and not the not :-) operator. If
it's not already type 'int', is there a conversion or not?

e.g.

FILE *f = 0;

if(f) /* does this compare a 'FILE*' or 'int' object with zero? */
;

-Mike
Nov 14 '05 #43
On Sat, 1 Jan 2005 22:51:05 UTC, "Mike Wahler" <mk******@mkwah ler.net>
wrote:

"Keith Thompson" <ks***@mib.or g> wrote in message
news:ln******** ****@nuthaus.mi b.org...
"Mike Wahler" <mk******@mkwah ler.net> writes:
"Bonj" <a@b.com> wrote in message news:33******** *****@individua l.net... [...]
> or better still
> if(fopen("xyz.t xt", "r") == NULL) ...

And now how will you refer to the stream?
>
>
> They both will work, but if(!fp) is less explicit,

IMO it's quite explicit. A test for true/false (zero/nonzero).

>as it's not actually a
> boolean value you're testing, it's a returned handle, and you're testing> whether it's NULL or not.

You're testing whether 'fp', after being converted to 'int',
is zero or not.


There's no conversion to int. The unary '!' operator tests whether
its operand compares equal to 0. For a pointer, comparison to 0 is
comparison to a null pointer constant.


But what is the type of the expression !fp?


The comparion is tone with the type of the variable (here a pointer to
FILE whatever that may be. The result is an int.
I was thinking in the wider context of 'if's (full)
conditional expression itself, and not the not :-) operator. If
it's not already type 'int', is there a conversion or not?
There is no conversion other than the implicit 0 gets converted to be
a null pointer constant. The result is generated as of type int.
e.g.

FILE *f = 0;

if(f) /* does this compare a 'FILE*' or 'int' object with zero? */
;


Comparion is in type of the variable. Result is generated in type of
int.

if (x) or in long if (x == 0)
is falling into 3 steps
1. x == (type of x)0; /* compare given variable with (type of x)0 */
2. if x is equal to (type of x)0 then (int)1 is generated, else (int)0
is generated.
3. conditional branch checks the result of the comparsion.

if (!x) or in long if (x != 0)
exchanges only the generated value.

It is on the implementation to suppress the explicite generation of
(int)0/1 and use processor flags of the comparsion or simply use a
test instruction. Depending on the instruction set it may even do
another optimision step and use an test/branch instruction. In no case
a conversation is needed or even done.

The same is with the ?: operator - except that the explicite 0/1 must
be generated when the result is needed for more than only to
distinguish the both possible results.

--
Tschau/Bye
Herbert

Visit http://www.ecomstation.de the home of german eComStation
eComStation 1.2 Deutsch ist da!
Nov 14 '05 #44
"Mike Wahler" <mk******@mkwah ler.net> writes:
"Keith Thompson" <ks***@mib.or g> wrote in message
news:ln******** ****@nuthaus.mi b.org...
"Mike Wahler" <mk******@mkwah ler.net> writes: [...]
> You're testing whether 'fp', after being converted to 'int',
> is zero or not.
There's no conversion to int. The unary '!' operator tests whether
its operand compares equal to 0. For a pointer, comparison to 0 is
comparison to a null pointer constant.


But what is the type of the expression !fp?


It's of type int.

C99 6.5.3.3p5:

The result of the logical negation operator ! is 0 if the value of
its operand compares unequal to 0, 1 if the value of its operand
compares equal to 0. The result has type int. The expression !E
is equivalent to (0==E).
I was thinking in the wider context of 'if's (full)
conditional expression itself, and not the not :-) operator. If
it's not already type 'int', is there a conversion or not?

e.g.

FILE *f = 0;

if(f) /* does this compare a 'FILE*' or 'int' object with zero? */
;


C99 6.8.4.1 p1-2:

Constraints

The controlling expression of an if statement shall have scalar type.

Semantics

In both forms, the first substatement is executed if the
expression compares unequal to 0. In the else form, the second
substatement is executed if the expression compares equal to 0.

Scalar types include arithmetic and pointer types.

So in

if (f) ...

the controlling expression has type FILE*. In

if (!f) ...

the operand of the "!" has type FILE*, and the controlling expression
has type int.

--
Keith Thompson (The_Other_Keit h) 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.
Nov 14 '05 #45
Herbert Rosenau wrote:
On Sat, 1 Jan 2005 22:51:05 UTC, "Mike Wahler" <mk******@mkwah ler.net>
wrote:

"Keith Thompson" <ks***@mib.or g> wrote in message
news:ln****** ******@nuthaus. mib.org...
"Mike Wahler" <mk******@mkwah ler.net> writes:

"Bonj" <a@b.com> wrote in message news:33******** *****@individua l.net...

[...]

>or better still
>if(fopen(" xyz.txt", "r") == NULL) ...

And now how will you refer to the stream?

>
>They both will work, but if(!fp) is less explicit,

IMO it's quite explicit. A test for true/false (zero/nonzero).
>as it's not actually a
>boolean value you're testing, it's a returned handle, and you're
testing
>whether it's NULL or not.

You're testing whether 'fp', after being converted to 'int',
is zero or not.

There's no conversion to int. The unary '!' operator tests whether
its operand compares equal to 0. For a pointer, comparison to 0 is
comparison to a null pointer constant.


But what is the type of the expression !fp?

The comparion is tone with the type of the variable (here a pointer to
FILE whatever that may be. The result is an int.

I was thinking in the wider context of 'if's (full)
conditional expression itself, and not the not :-) operator. If
it's not already type 'int', is there a conversion or not?

There is no conversion other than the implicit 0 gets converted to be
a null pointer constant. The result is generated as of type int.

e.g.

FILE *f = 0;

if(f) /* does this compare a 'FILE*' or 'int' object with zero? */
;

Comparion is in type of the variable. Result is generated in type of
int.

if (x) or in long if (x == 0)


if (x) is equivalent to if (x != 0)
is falling into 3 steps
1. x == (type of x)0; /* compare given variable with (type of x)0 */
2. if x is equal to (type of x)0 then (int)1 is generated, else (int)0
is generated.
3. conditional branch checks the result of the comparsion.

if (!x) or in long if (x != 0)
if (!x) is equivalent to if (x == 0)
exchanges only the generated value.

It is on the implementation to suppress the explicite generation of
(int)0/1 and use processor flags of the comparsion or simply use a
test instruction. Depending on the instruction set it may even do
another optimision step and use an test/branch instruction. In no case
a conversation is needed or even done.

The same is with the ?: operator - except that the explicite 0/1 must
be generated when the result is needed for more than only to
distinguish the both possible results.


Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Nov 14 '05 #46
On Sun, 2 Jan 2005 08:46:10 UTC, Michael Mair
<Mi**********@i nvalid.invalid> wrote:
if (!x) or in long if (x != 0)


if (!x) is equivalent to if (x == 0)


No, it's not, it is highly different.
--
Tschau/Bye
Herbert

Visit http://www.ecomstation.de the home of german eComStation
eComStation 1.2 Deutsch ist da!
Nov 14 '05 #47


Herbert Rosenau wrote:
On Sun, 2 Jan 2005 08:46:10 UTC, Michael Mair
<Mi**********@i nvalid.invalid> wrote:
if (!x) or in long if (x != 0)


if (!x) is equivalent to if (x == 0)


No, it's not, it is highly different.


Don't be ridiculous.

-----------------------
#include <stdio.h>

int main (void)
{
double f = 0.0;
char *cp = 0;
int i = 0;

if (!i)
puts("!i");
if (i==0)
puts("i==0");
if (!cp)
puts("!cp");
if (cp==0)
puts("cp==0");
if (!f)
puts("!f");
if (f==0)
puts("f==0");

return 0;
}
-----------------------

gives me:
!i
i==0
!cp
cp==0
!f
f==0
Cheers
Michael
--
E-Mail: Mine is a gmx dot de address.

Nov 14 '05 #48
Herbert Rosenau wrote on 02/01/05 :
On Sun, 2 Jan 2005 08:46:10 UTC, Michael Mair
<Mi**********@i nvalid.invalid> wrote:
if (!x) or in long if (x != 0)


if (!x) is equivalent to if (x == 0)


No, it's not, it is highly different.


In what manner ? Can you be more specific ?

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"C is a sharp tool"

Nov 14 '05 #49
"Herbert Rosenau" <os****@pc-rosenau.de> wrote in message
news:wm******** *************** ****@URANUS1.DV-ROSENAU.DE...
On Sun, 2 Jan 2005 08:46:10 UTC, Michael Mair
<Mi**********@i nvalid.invalid> wrote:
if (!x) or in long if (x != 0)


if (!x) is equivalent to if (x == 0)


No, it's not, it is highly different.


It is?

According to N869 6.5.3.3(5), they are equivalent:

"The result of the logical negation operator ! is 0 if the value of its
operand compares unequal to 0, 1 if the value of its operand compares equal
to 0. The result has type int. The expression !E is equivalent to (0==E). "

S

--
Stephen Sprunk "Stupid people surround themselves with smart
CCIE #3723 people. Smart people surround themselves with
K5SSS smart people who disagree with them." --Aaron Sorkin

Nov 14 '05 #50

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1800
by: mikeg | last post by:
Hi, I'd like to explicitly link to the recommended php.ini file. For a long time, this link worked: http://cvs.php.net/co.php/php4/php.ini-recommended (In fact, if you google for "php ini recommended", that broken link comes up first).
6
2264
by: Huy | last post by:
Generic PHP newbie question here. May anyone please recommend books that have gotten you started on PHP (interfacing with MySQL is a plus) & also reference books? Just looking for some good quality books to purchase.
13
2395
by: Michele Simionato | last post by:
What is the recommended way of generating HTML from Python? I know of HTMLGen and of few recipes in the Cookbook, but is there something which is more or less standard? Also, are there plans to include a module for HTML generation in the standard library? I really would like to see some standardization in this area. Michele Simionato
7
4392
by: Mike Kamermans | last post by:
I hope someone can help me, because what I'm going through at the moment trying to edit XML documents is enough to make me want to never edit XML again. I'm looking for an XML editor that has a few features that you'd expect in any editor, except nearly none of them seem to have: 1 - Search and repalce with Regular Expressions. 2 - Search and Replace in an Xpath context. 3 - User specified tag-generation for either on a...
22
5150
by: Jonathan Snook | last post by:
I've been contemplating what the recommended usage of a "top of page" link should be? Should there only ever be one at the bottom of the page? Should they be sprinkled at various points on the page? Or should they be used at all? Lately, I've been leaning towards the last option because my thought is that most browsers have a method to make it back to the top of the page (home button, scroll bar, whatever). It seems I never use the...
11
1643
by: G Patel | last post by:
Can anways explain why many multi-file programs I've seen don't have a main.c file, instead main() is hidden in a file with a different name? Is this just a preference, or is there an unspoken rule (or advantage). I've been calling the file with main, main.c, and I was wondering if there are any caveats against this (that might prevent me from being laughed at or something).
3
1777
by: darren via AccessMonster.com | last post by:
Hi I'm based in the UK and I've drifted into Access from building a simple db for myself, to then being asked to build a simple db for someone else, to now spending time building increasingly more sophisticated (for me)databases. So far my learning curve has been based upon a handleful of books and this forum (which I think is fantastic and the level of help and knowledge sharing has astounded me). Aside from the very basic dummies...
2
15497
by: Mark Rae | last post by:
Hi, I've been asked to "update" an existing fairly simple website to make it XHTML-compliant i.e. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > ... </html>
13
2105
by: Michael M. | last post by:
In Perl, it was: ## Example: "Abc | def | ghi | jkl" ## -"Abc ghi jkl" ## Take only the text betewwn the 2nd pipe (=cut the text in the 1st pipe). $na =~ s/\ \|(.*?)\ \|(.*?)\ \|/$2/g; ## -- remove in text
0
9484
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10350
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10157
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9957
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5386
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5518
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4055
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3658
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2887
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.