Find the output of the following C Apptitude

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

  • dipen30
    dipen30
    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
    yes, dipen gave the correct answer...!

    Its 7.
  • sarveshgupta
    sarveshgupta
    Isn't it 6 ?

    Because the first condition itself is not true that is ++k is not < 5
  • dipen30
    dipen30
    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
    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
    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
    Sir,
    Thanks for your valuable answer.
    R.Rengaraj
  • sarveshgupta
    sarveshgupta
    rengaraj
    Sir,
    Thanks for your valuable answer.
    R.Rengaraj
    But what is the right answer?
  • gaurav.bhorkar
    gaurav.bhorkar
    Dipen, your first answer is right and you have explained it correctly. The answer is 7.
  • dipen30
    dipen30
    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
    sarveshgupta
    But what is the right answer?
    Sir,
    I hope 7 is the correct answer
    R.Rengaraj
  • sarveshgupta
    sarveshgupta
    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
    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
    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
    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
    @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
    What is after being used?

    I didnt get!!
  • sarveshgupta
    sarveshgupta
    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
    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
    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.

You are reading an archived discussion.

Related Posts

Google is advertising on Microsoft's Bing search engine ๐Ÿ˜: A picture is worth thousand words: Source: TechCrunch
Google may launch the Chrome extension to everyone next week. Currently the extensions site is in private beta. Do you think Chrome Extensions will ensure a tough competition to Industry...
We all like to take photos. Photography is a skill and I believe with the right knowledge, everyone can learn to take great shorts. One of the most challenging task...
Hi i'm jijo, Please help in finding a topic for my final year Btech project on cryogenic nitrogen plant.
India has finally been able to claim the numero uno position after so many years They have deserved it from long ago, struggled for it and put in more and...