Find the output of the following C Apptitude
Sir / madam,
My question is
Find the output of the following
R.Rengaraj
{Java Certification Quiz}
{On this forum my 21st Question.}
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
-
dipen30Ans 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 Pallaviyes, dipen gave the correct answer...!
Its 7. -
sarveshguptaIsn't it 6 ?
Because the first condition itself is not true that is ++k is not < 5 -
dipen30In 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. -
sarveshguptaYes 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 -
dipen30Program 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. -
rengarajSir,
Thanks for your valuable answer.
R.Rengaraj -
sarveshgupta
But what is the right answer?rengarajSir,
Thanks for your valuable answer.
R.Rengaraj -
gaurav.bhorkarDipen, your first answer is right and you have explained it correctly. The answer is 7.
-
dipen30sarveshgupta ,
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
Sir,sarveshguptaBut what is the right answer?
I hope 7 is the correct answer
R.Rengaraj -
sarveshguptaBut 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? -
dipen30first 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. -
sarveshguptaok 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 SreerambatlaHey 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@ES In post increment the value gets incremented after being used for that iteration
So it will be incremented after being used -
Saandeep SreerambatlaWhat is after being used?
I didnt get!! -
sarveshguptaAfter 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 Goyalyeah 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 FarokeI 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...