CrazyEngineers
  • Find the output of the following C Apptitude

    rengaraj

    rengaraj

    @rengaraj-89Rrct
    Updated: Oct 16, 2024
    Views: 930
    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.}
    0
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • dipen30

    MemberDec 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..
    Are you sure? This action cannot be undone.
    Cancel
  • Sahithi Pallavi

    MemberDec 5, 2009

    yes, dipen gave the correct answer...!

    Its 7.
    Are you sure? This action cannot be undone.
    Cancel
  • sarveshgupta

    MemberDec 5, 2009

    Isn't it 6 ?

    Because the first condition itself is not true that is ++k is not < 5
    Are you sure? This action cannot be undone.
    Cancel
  • dipen30

    MemberDec 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.
    Are you sure? This action cannot be undone.
    Cancel
  • sarveshgupta

    MemberDec 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
    Are you sure? This action cannot be undone.
    Cancel
  • dipen30

    MemberDec 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.
    Are you sure? This action cannot be undone.
    Cancel
  • rengaraj

    MemberDec 6, 2009

    Sir,
    Thanks for your valuable answer.
    R.Rengaraj
    Are you sure? This action cannot be undone.
    Cancel
  • sarveshgupta

    MemberDec 6, 2009

    rengaraj
    Sir,
    Thanks for your valuable answer.
    R.Rengaraj
    But what is the right answer?
    Are you sure? This action cannot be undone.
    Cancel
  • gaurav.bhorkar

    MemberDec 6, 2009

    Dipen, your first answer is right and you have explained it correctly. The answer is 7.
    Are you sure? This action cannot be undone.
    Cancel
  • dipen30

    MemberDec 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.
    Are you sure? This action cannot be undone.
    Cancel
  • rengaraj

    MemberDec 6, 2009

    sarveshgupta
    But what is the right answer?
    Sir,
    I hope 7 is the correct answer
    R.Rengaraj
    Are you sure? This action cannot be undone.
    Cancel
  • sarveshgupta

    MemberDec 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?
    Are you sure? This action cannot be undone.
    Cancel
  • dipen30

    MemberDec 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.
    Are you sure? This action cannot be undone.
    Cancel
  • sarveshgupta

    MemberDec 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
    Are you sure? This action cannot be undone.
    Cancel
  • Saandeep Sreerambatla

    MemberDec 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 !!!
    Are you sure? This action cannot be undone.
    Cancel
  • sarveshgupta

    MemberDec 7, 2009

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

    So it will be incremented after being used
    Are you sure? This action cannot be undone.
    Cancel
  • Saandeep Sreerambatla

    MemberDec 7, 2009

    What is after being used?

    I didnt get!!
    Are you sure? This action cannot be undone.
    Cancel
  • sarveshgupta

    MemberDec 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
    Are you sure? This action cannot be undone.
    Cancel
  • Manish Goyal

    MemberDec 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
    Are you sure? This action cannot be undone.
    Cancel
  • Hussanal Faroke

    MemberDec 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.
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register