C output type question

Q.  C programming : Trace the output.

void main()
{
    int i=2,j=2;
    while(i+1?--i:j++)
         printf("%d",i);    
}

- Published on 19 Oct 15

a. 1
b. 2
c. ERROR
d. None of the above.

ANSWER: 1
 

    Discussion

  • Vishwesh   -Posted on 11 Oct 16
    read parenthesis as"if i+1 is true then --i else j++"
    here i+1 =3 which is non zero and anything non zero in C is referred as true so condition becomes and thus --i is executed giving i=1
    later printf prints the new value of i which is 1.

    I hope this helps.
  • dolly yadav   -Posted on 30 Apr 16
    sir
    i request for you .can you explanation this question and dry run

Post your comment / Share knowledge


Enter the code shown above:

(Note: If you cannot read the numbers in the above image, reload the page to generate a new one.)