On Nov 10, 2007 11:01 PM, 王磊 <[EMAIL PROTECTED]> wrote:
> What is the meaning of
>
>
>
> int count;
>
> 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 (an easy way to see if a number
is odd or even). This statement is used for the conditional part of
the tertiary operator (?:) which is essentially the same as an if-else
statement:

if(count % 2)   //if count is not divisible by 2 (odd)
  printf("%s", "*");
else               //count is even
  printf("%s", "+");

The tertiary operater condenses this down to

   count % 2 ? "*" : "+"

So, the first part of the sequence evaluates to boolean false or
boolean true, if true (non-negative integer), return the value between
? and : otherwise (0), return the value after :

-- Brett
------------------------------------------------------------
"In the rhythm of music a secret is hidden;
    If I were to divulge it, it would overturn the world."
               -- Jelaleddin Rumi

Reply via email to