Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

What does y = a ? 5: a++ mean in C coding?

In C programming what does y equal for the following code:

int a =0;

y = a ? 5:++a;

I cant get my head around the ? symbol ive never seen it before and im unsure :/

Please help!

3 Answers

Relevance
  • Chris
    Lv 7
    7 years ago

    The syntax is

    variable = condition ? new_value_if_true : new_value_if_false;

    Another example:

    String sign = x > 0 ? "positive" : "negative";

    a used as condition evaluates to false (0 -> false, everything else -> true), therefore the line sets y to ++a, which means a is incremented by one first, then its value is used. So y ends up being 1.

  • 7 years ago

    int a=0;

    if (a)

    {

    y=5;

    }

    else {

    y= 1;

    }

  • 7 years ago

    it means if a is true then 5

    else 0;

    since initially a=0 so a= false

    also since ++a it means y will take y=a=0; as preincremented.

    after interpretation y=1;

Still have questions? Get your answers by asking now.