Article View: comp.std.c
Article #3010Re: float to cond conversions
From: Larry Jones
Date: Sat, 12 Dec 1992 20:11
Date: Sat, 12 Dec 1992 20:11
44 lines
2100 bytes
2100 bytes
In article <1992Dec09.011729.20954@Veritas.COM>, joshua@Veritas.COM (Joshua Levy) writes:
> Consider the following code:
> int same(int b) { return b ; }
> if (X)
> if (same(!!(X)))
> if (same((int)(X))))
> X can be any legal C code including something like "f == 4.0"
> where f is a float or a double.
>
> If I have an ANSI C compiler, are either of the second two if statements
> identical in behavior to the first if statement?
Yes, the second one is.
First, note that your same() function is, at best, a long-winded way to
write an (int) cast and, at worst, a noop. Thus, the second and third
if statements are equivalent to:
if ((int)!!(X))
if ((int)(int)(X))
Since the result of the ! operator is already an int and you already had
an explicit (int) cast in the third if statement, same() is a complete
noop in both of the cases which then degerate to:
if (!!(X))
if ((int)(X))
Now, let's write out the implicit comparisons with 0 explicitly:
if ((X) != 0)
if (!((X) == 0)) => if ((X) != 0)
if ((int)(X) != 0)
It's now obvious that the first and second statements are identical and
the third satement is also identical *if X has an integral value*. If X
has a non-integral value like 0.1, the first two statements will be true
but the third will be false.
> I'm also interested in knowing if either of the second two if statements
> will have different behavior in any common K&R C compiler.
No, but a K&R compiler will not automatically convert the argument to
same() to int the way an ANSI compiler will (given a prototype).
----
Larry Jones, SDRC, 2000 Eastman Dr., Milford, OH 45150-2789 513-576-2070
larry.jones@sdrc.com or ...uunet!sdrc!larry.jones
I sure wish I could get my hands on some REAL dynamite. -- Calvin
Technical Details
Message-ID:
<2388@sdrc.COM>Path:
arc1-ca.pugleaf.net!sparky!uunet!sdrc!thor!scjonesReferences:
<1992Dec09.011729.20954@Veritas.COM>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!sdrc!thor!scjones From: scjones@thor.sdrc.com (Larry Jones) Newsgroups: comp.std.c Subject: Re: float to cond conversions Message-ID: <2388@sdrc.COM> Date: 12 Dec 92 20:11:49 GMT References: <1992Dec09.011729.20954@Veritas.COM> Sender: news@sdrc.COM Lines: 44