M
Martin Johansen
if(a);
In this code, towhat type does the if statement cast the variable "a" to on
comparizon?
In this code, towhat type does the if statement cast the variable "a" to on
comparizon?
if(a);
In this code, towhat type does the if statement cast the variable "a" to on
comparizon?
Martin said:if(a);
In this code, towhat type does the if statement cast the variable "a" to on
comparizon?
Mark McIntyre said:Back to your original question: 'a' has to be an integral type, or
something that can be converted to an integral type.
a isn't cast to any type. A cast is when the programmer deliberately
types extra stuff eg
(sometype) foo;
casts foo to a sometype object.
Back to your original question: 'a' has to be an integral type, or
something that can be converted to an integral type.
like a cast" without the explicit "(sometype)" is an "implicit
conversion."
Martin Johansen said:Ok i see and accept, but just for the record, why not call it a cast? It was
implied that it was an implicit action.
[NB: xpost to comp.std.c added]
a isn't cast to any type. A cast is when the programmer deliberately
types extra stuff eg
(sometype) foo;
casts foo to a sometype object.
For the OP's benefit: The correct term for something that "acts
like a cast" without the explicit "(sometype)" is an "implicit
conversion."
Back to your original question: 'a' has to be an integral type, or
something that can be converted to an integral type.
As Ben said, not quite. 'a' has to be a type which can be
validly compared for equality with 0. That is, the line
if (a) ;
is exactly equivalent to the line
if ((a) == 0) ;
no matter what the type of 'a' is. If 'a' doesn't have the right
type for that second line to make sense, then the first line is
invalid as well.
Crosspost to c.s.c added because of the following...
Question for the experts: Does the C standard clarify what it
means by "compare equal to 0" anywhere? Is that 0 the literal 0
of type 'int', or the value you get by converting 0 to the type
of 'a'? Is there any valid C program that could tell the difference?
-Arthur
Arthur J. O'Dwyer said:As Ben said, not quite. 'a' has to be a type which can be
validly compared for equality with 0. That is, the line
if (a) ;
is exactly equivalent to the line
if ((a) == 0) ;
no matter what the type of 'a' is.
Arthur said:Question for the experts: Does the C standard clarify what it
means by "compare equal to 0" anywhere?
If you specifically code:
if (a == 0)
...where a is any scalar type, the following will occur.
The type of the octal signed int literal 0 will be compared to the
type of 'a'.
3. 'a' has an integer type of lesser rank than signed int. If 'a' is
an unsigned type, and the entire range of values of that unsigned type
cannot be represented in a signed int, 'a' and 0 are both promoted to
unsigned int.
Question for the experts: Does the C standard clarify what it
means by "compare equal to 0" anywhere? Is that 0 the literal 0
of type 'int', or the value you get by converting 0 to the type
of 'a'?
Is there any valid C program that could tell the difference?
Martin said:Ok i see and accept, but just for the record, why not call it a cast? It was
implied that it was an implicit action.
Dan said:Note to the newbies following this discussion: unless a is used as a
conceptual boolean (i.e. a flag) it's preferrable NOT to omit the
explicit comparison and to use NULL instead of 0 when testing pointers.
In said:In *your* opinion. My eyes parse if(p) quicker than if(p != NULL).
Dan said:What is not a matter of opinion is that if(p != NULL) is *correctly*
parsed by *any* reader, while if(p) isn't (otherwise there wouldn't be
a FAQ question dedicated to this very issue in the first place).
Therefore, it is a *fact* that if(p != NULL) is more readable than if(p).
Wojtek Lerch said:Yes, but only according to your definition of readability, based on the
number of people on the planet who can correctly parse the given piece
of code. Apparently, in your opinion that's an appropriate definition
of readability in this context. But in some other people's opinion, it
may be less important how easily a completely newbie can misunderstand
our code, and more important how efficiently an experienced person can
read it.
In said:Yes, but only according to your definition of readability, based on the
number of people on the planet who can correctly parse the given piece
of code. Apparently, in your opinion that's an appropriate definition
of readability in this context. But in some other people's opinion, it
may be less important how easily a completely newbie can misunderstand
our code, and more important how efficiently an experienced person can
read it.
In short, whether something is preferrable or not depends on who prefers it.
Dan said:I have yet to see proof that it makes *any* difference to the *competent*
programmers but I have already seen proof that *experienced* programmers
may not fully and correctly understand the short form. Furthermore, the
code may be written by a beginner who has the wrong idea about the effect
of omitting the explicit comparison... The explicit comparison simply
removes all kinds of doubts.
> (d+e)) && (f > g)".
Not when objective criteria are used.
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.