"Brett McCoy" <[EMAIL PROTECTED]> wrote:
> ²¦âý <[EMAIL PROTECTED]> wrote:
> > printf ("%s\n", count % 2 ? "*" : "+");
> 
> count % 2 is using the modulo operator -- it gives you the
> remainder for integer division. If count is divisible by two,
> the statement will return 0, otherwise it will return 1

...or -1 depending on the sign of count and whether the
implementation rounds integer division towards zero.
[C99 requires this, C90 does not, though most C90
implementations do.]

If you explicitly want 1 for odd numbers, then use
(count % 2 != 0).

> (an easy way to see if a number is odd or even).

Yes, because -1 is still non-zero and so won't give false
positives for even numbers.

> This statement is used for the conditional part of the
> tertiary operator (?:) which is essentially the same as
> an if-else statement:

True, but the analogy can be stretched too far in some
situations. For instance, if s is an unsigned long, then...

  s = (expr) ? 1u : -1;

...is not the same as...

  if (expr)
    s = 1u;
  else
    s = -1;

If expr is false, the first will put UINT_MAX into s, the
second will put ULONG_MAX into s.

-- 
Peter

Reply via email to