Re: if(a);

From: Arthur J. O'Dwyer (ajo_at_nospam.andrew.cmu.edu)
Date: 05/11/04


Date: Mon, 10 May 2004 19:54:26 -0400 (EDT)


[NB: xpost to comp.std.c added]

On Mon, 10 May 2004, Mark McIntyre wrote:
>
> Martin Johansen wrote:
> > if(a);
> >
> > In this code, to what type does the if statement cast the variable
> > "a" on comparison?
>
> 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



Relevant Pages

  • Re: Terminology : casting/conversion
    ... Is this an "explicit" cast? ... conversion even if the type of the expression is the ... Here are p & p2 are pointers but the compiler "implicitly" converts ... this parse tree is almost unconnected to the ...
    (comp.lang.c)
  • Re: Collections
    ... cast an abstract class into a derived class ... First remember that DirectCast is the Cast operator, while CType is the ... Conversion operator that will do a Cast if the cast is allowed. ... remember that Casting & Converting are separate ...
    (microsoft.public.dotnet.languages.vb)
  • Re: C variable retyping
    ... whose type is given by the cast. ... >an integer, no conversion is done, and c is used, within the ... In other words, you may take a pointer value, then use a cast to ... which is probably why the C99 folks decided to allow it ...
    (comp.lang.c)
  • Re: Function Pointers
    ... Just a heads-up - you don't need the cast. ... lossless conversion between void * and function pointers, ... I thought you couldn't have a generic function pointer like the 'void ... there is no generic function pointer type. ...
    (comp.lang.c)
  • Re: Cast to a struct?
    ... Neither the source expression nor the target type of a cast is ... allowed to be a struct (except that any expression can be cast to ... A cast specifies a type conversion. ... unsigned int is exactly 32 bits. ...
    (comp.lang.c)