Find the output for the following code
Sir / madam,
My question is
Find the output for the following code
void main()
{
int i=7;
printf("%d",i++*i++);
}
Advance Thanks,
R.Rengaraj
{Java Certification Quiz}
{On this forum my 20th Question.}
My question is
Find the output for the following code
void main()
{
int i=7;
printf("%d",i++*i++);
}
Advance Thanks,
R.Rengaraj
{Java Certification Quiz}
{On this forum my 20th Question.}
Replies
-
sarveshguptaIs the answer 56?
-
rengarajhi SarveshGupta,
Your answer is right, but i am confused with the answer. Can you explain me the process ?
R.Rengaraj -
sarveshguptai++ means using post increment that is when we use it in any expression it uses the current value of that variable that is being carried forward from previous statement and then increment after compiler reads it so that any next time compiler reads the variable again it will read the incremented value
now we are doing i++ * i++ this implies
first time the value read is 7 which was the initial value of i and it gets incremented so for next operand i++ it takes the value 8 and after using 8 it increments it to 9
since we are using them only till here we get the value as 7*8 =56
if we try to print the value of i at the end of the program it will be 9
void main() { int i=7; printf("%d \n",i++*i++); printf("%d",i); }
will give the output as:
56
9 -
gaurav.bhorkar@sarvesh
You explained it perfectly, buddy. -
rengaraj
Dear sarveshgupta,sarveshguptai++ means using post increment that is when we use it in any expression it uses the current value of that variable that is being carried forward from previous statement and then increment after compiler reads it so that any next time compiler reads the variable again it will read the incremented value
now we are doing i++ * i++ this implies
first time the value read is 7 which was the initial value of i and it gets incremented so for next operand i++ it takes the value 8 and after using 8 it increments it to 9
since we are using them only till here we get the value as 7*8 =56
if we try to print the value of i at the end of the program it will be 9
void main() { int i=7; printf("%d \n",i++*i++); printf("%d",i); }
will give the output as:
Your answer is right, but you stated as 7*8 why i can be 8*7,
bcoz, if execution is being handled from right to left.
R.Rengaraj -
gaurav.bhorkar
Good Point. In fact printf statement handles execution from right to left (in C).rengarajDear sarveshgupta,
Your answer is right, but you stated as 7*8 why i can be 8*7,
bcoz, if execution is being handled from right to left.
R.Rengaraj
In this case it won't affect the answer in either case. -
Sahithi Pallavi@sarvesh : Very good explanation.!
@rengaraj : Nice point..! -
rengarajSir,
Thanks to all of you. For your excellent co-operation.
R.Rengaraj -
sarveshguptaYa I am sorry for writing it that way but i actually meant 8 * 7 as I know printf reads from right to left
I wrote it simply missing the order of writing -
rengarajNo, Problem Sarveshgupta
You are reading an archived discussion.
Related Posts
Firefox mainly survives because of the different Add-ons / Extensions and now Google Chrome supporting this would mean that the market share is going to boost in the year 2010....
Hello CEs, it's my first post, this web site is very interesting, i knew that i should introduce my self, so let's start:
My Name: Hussein Barakat
i'm in the...
Harpreet Singh Bedi (Ranbir Kapoor) just about scrapes through college with no clue of what he is going to do next. Unable to do an MBA due to poor scores...
MediaMonkey
Version: 3.2.0.1294
File Size: 7621
License: Free
Operating System: Windows XP,Windows Vista,Windows 7
If you've got a gargantuan media collection in dire need of
reorganization, MediaMonkey is a must....
Industry grew by a robust 10.3 per cent in October against a paltry 0.1 per cent a year ago, powered by manufacturing, particularly consumer durables, which was driven by the...