Re: When are unnecessary casts useful?



"jacob navia" <jacob@xxxxxxxxxxxxxxxx> wrote in message
news:45a395ad$0$25906$ba4acef3@xxxxxxxxxxxxxxxxx

To avoid the warning you have to explicitely cast the LHS to meet
the RHS, like this:

a = (char)b;

The cast makes your intentions clear.

The same approach exists in Microsoft compilers.

I have gotten used to do this, more or less automatically when I
cast "down", i.e. from a bigger to a smaller type.

This comes down to one of the oldest arguments with programming, especially
with 'C'. You should look at Les Hatton's classic work, "Safer C".

Even in areas of the language that are well-defined (i.e. the compiler won't
make mistakes); statistically, humans make mistakes.

You might find in a lot of corporate coding standards the requirement that
if() conditions, be fully parenthesized, i.e.

if ((!a) || (*a > 10))

An experienced programmer will argue this requirement is rubbish (i.e. any
programmer should know precedence and order of evaluation), whereas
management would argue either that (a)it is an area where people are prone
to make mistakes and/or (b)it becomes relevant when there is a team at a
mixed skill level.

This thread and all its derivatives is older than time. (Or, at least it
dates back to nearly 1972.)


.



Relevant Pages

  • Re: Common misconceptions about C (C95)
    ... Cast it to suitable type instead. ... workers by workers, where the workers with the least principle become ... But to make a programmer code to show off a knowledge of what Kenny ...
    (comp.lang.c)
  • Re: warning - comparing a signed value to an unsinged value
    ... but only to the compiler -- more below. ... Casting i gets rid of the warning; adding the redundant ... I would still cast i in ... Even if the programmer in question is someone like you, ...
    (comp.lang.c)
  • Re: [OT] C++ question re. dyn. mem.
    ... MJM> aware of what you are doing when you do a type conversion. ... wanted to strike the C-style cast out of the C++ standard except that all C ... intelligence" is in general no good, since the programmer probably only ...
    (Debian-User)
  • Re: Generics is a waste of time
    ... > object is being dealt with and thus what appropriate cast ... > as a programmer? ... Getting around that cast would have been quite simple taking care of that at ... attention, favouring more quality, which I'm far from sure of. ...
    (comp.lang.java.programmer)
  • Re: pointer type casting
    ... especially the cast of pointer type. ... that float object to that unsigned long object. ... pointer casts tells me that the programmer was deliberately trying to ...
    (comp.lang.c)

Loading