Find the output of the following C Apptitude

rengaraj

rengaraj

@rengaraj-89Rrct Oct 16, 2024
Sir / madam,
My question is

Find the output of the following

main()
{
        int k = 5;
        if (++k < 5 && k++/5 || ++k <= 8);
        printf("%d\n", k);
}
Advance Thanks,
R.Rengaraj
{Java Certification Quiz}
{On this forum my 21st Question.}

Replies

Welcome, guest

Join CrazyEngineers to reply, ask questions, and participate in conversations.

CrazyEngineers powered by Jatra Community Platform

  • dipen30

    dipen30

    @dipen30-hGOPpa Dec 5, 2009

    Ans is 7.

    first (++k<5) here k is pre-increment so that value of k is 6.

    then (k++/5) here k is post-increment so value of k is remain 6 because in post-increment
    value increment after being used.

    and (++k<=8) here k is pre-increment so the value of k is 7.

    correct me if I am wrong..
  • Sahithi Pallavi

    Sahithi Pallavi

    @sahithi-oJZaYj Dec 5, 2009

    yes, dipen gave the correct answer...!

    Its 7.
  • sarveshgupta

    sarveshgupta

    @sarveshgupta-txtmu5 Dec 5, 2009

    Isn't it 6 ?

    Because the first condition itself is not true that is ++k is not < 5
  • dipen30

    dipen30

    @dipen30-hGOPpa Dec 6, 2009

    In pre-increment value incremented by 1 before variable using it. and Here we are not return or print any value after condition check.

    you can see semicolon( ; ) after if condition.
  • sarveshgupta

    sarveshgupta

    @sarveshgupta-txtmu5 Dec 6, 2009

    Yes dipen but you have print statement after the if iteration

    that is the program will not enter if and directly print the value of k

    which was once pre incremented by 1 in checking the first condition
  • dipen30

    dipen30

    @dipen30-hGOPpa Dec 6, 2009

    Program will check if condition but it see semicolon after condition it exit from if condition.

    and when it comes to statement is print the last value of k which is incremented during if condition.
  • rengaraj

    rengaraj

    @rengaraj-89Rrct Dec 6, 2009

    Sir,
    Thanks for your valuable answer.
    R.Rengaraj
  • sarveshgupta

    sarveshgupta

    @sarveshgupta-txtmu5 Dec 6, 2009

    rengaraj
    Sir,
    Thanks for your valuable answer.
    R.Rengaraj
    But what is the right answer?
  • gaurav.bhorkar

    gaurav.bhorkar

    @gauravbhorkar-Pf9kZD Dec 6, 2009

    Dipen, your first answer is right and you have explained it correctly. The answer is 7.
  • dipen30

    dipen30

    @dipen30-hGOPpa Dec 6, 2009

    sarveshgupta ,

    here in if condition first it check ++k<5 at that time value of k is 6 & condition become false
    then it check k++/5 at that time value of k is 6 and condition become false.

    now if condition looks like if(0 && 0 || ++k<=8).
    (0 && 0) = 0 therefore now if(0 || ++k<=8).

    now if check ++k<=8 at that time value of k is 7 and condition is true so if condition looks like if(0 || 1). and (0 || 1) = 1. but after if condition there is a semicolon so it exit from if condition.

    but when it comes to printf it print the value of k which is 7 because scope of k is for entire main() function.
  • rengaraj

    rengaraj

    @rengaraj-89Rrct Dec 6, 2009

    sarveshgupta
    But what is the right answer?
    Sir,
    I hope 7 is the correct answer
    R.Rengaraj
  • sarveshgupta

    sarveshgupta

    @sarveshgupta-txtmu5 Dec 6, 2009

    But Dipen as you can see we have && and in case of boolean 'and' if the first condition is false it does nto check for the second condition it automatically exits the iteration so as soon as first time the value of k gets incremented it exits if and prints the then value of k which is 6

    So it should be 6

    @rengaraj : you are saying you hope that 7 is correct answer aren't you sure what the correct answer is?
  • dipen30

    dipen30

    @dipen30-hGOPpa Dec 6, 2009

    first condition is false but what about ||(OR).

    if check all the condition whether the fist condition is true or false.

    and if you have C compiler then compile and run this code and see the output.
  • sarveshgupta

    sarveshgupta

    @sarveshgupta-txtmu5 Dec 7, 2009

    ok if you have compiled the code in C compiler to see the output then you must be right

    But i had thought that property of && is to check first condition first if it is true then only it moves to second condition irrespective of where it is placed

    But as you say the output should come this its fine
  • Saandeep Sreerambatla

    Saandeep Sreerambatla

    @saandeep-sreerambatla-hWHU1M Dec 7, 2009

    Hey guys I have a doubt here!

    First ++k then k becomes 6.

    Then comes k++/5 so its the post increment.

    So will the K incremented after the operation or not??

    If yes it should become 7 correct?

    Then we have ++k so k should be 8.

    As you people executed in C compiler the output is 7 as you said,.

    Please explain why it is 7 and not 8 !!!
  • sarveshgupta

    sarveshgupta

    @sarveshgupta-txtmu5 Dec 7, 2009

    @ES In post increment the value gets incremented after being used for that iteration

    So it will be incremented after being used
  • Saandeep Sreerambatla

    Saandeep Sreerambatla

    @saandeep-sreerambatla-hWHU1M Dec 7, 2009

    What is after being used?

    I didnt get!!
  • sarveshgupta

    sarveshgupta

    @sarveshgupta-txtmu5 Dec 9, 2009

    After being used means that for the current statement the value that is being carried over from previous statement is used

    It is only after the current statement that the value increments and the subsequent statements see the incremented value
  • Manish Goyal

    Manish Goyal

    @manish-r2Hoep Dec 18, 2009

    yeah Dipen is right answer should be 7 here..simply because since a ; lies at end of if statement so there will be no effect of if statement on output of program but since if state will execute it will change the value of k..do post and pre increment operator
  • Hussanal Faroke

    Hussanal Faroke

    @hussanal-faroke-U5nNM8 Dec 19, 2009

    I think answer 7 is right but it given by, first compiler do ++k<5, hence k becomes 6. due to the condition is false AND operator never do second expression. then compiler go to rightside of or and do that expression. at the time of this k incrimented once more and come to the line printf.