|  | |
11-30-2005, 02:47 PM
|
#1 (permalink)
|
True Techie Join Date: Nov 2005 Posts: 115
| small but odd! we have this written paper on c programin, and these were some which stumped me,
......
......
int j=1,q;
q=j++ + ++j + j++;
printf("%d %d",q,j);
getch();
clrscr();
q=j++ + ++j + ++j;
printf("%d %d",q,j);
getch();
}
think about the output, and check it out,
if you got how these compilers work, let me know.
i tried it on my comp later, and got values that baffle me, how the **** does it give me that output |
| |
11-30-2005, 04:06 PM
|
#2 (permalink)
|
It's all just 1s and 0s Join Date: Jan 2004 Location: in the lab Posts: 4,410
| hmmm,
int j=1,q; // j=1 q=null
q=j++ + ++j + j++; // translates to q= 2 + 2 + 2 (2 cus of pre inc) and j now == 2
// j=4 (cus of post inc) and q= 6
....
q=j++ + ++j + j++; // translates to q= 5 + 5 + 5, j == 5
// j == 7 and q == 15
i could be wrong about the first time j appears though. |
| |
11-30-2005, 04:11 PM
|
#3 (permalink)
|
True Techie Join Date: Nov 2005 Posts: 115
| ok try
q= ++j + j++ + ++j; |
| |
11-30-2005, 04:18 PM
|
#4 (permalink)
|
True Techie Join Date: Nov 2005 Posts: 115
| the one to j++ + ++j + j++ is wrong, its not 7 and 15, but the first one is correct |
| |
11-30-2005, 04:29 PM
|
#5 (permalink)
|
True Techie Join Date: Nov 2005 Posts: 115
| oh and the consider the 2 statements....as seperate ones, reset values of j and q! so sorry, for that confusion! |
| |
11-30-2005, 07:18 PM
|
#6 (permalink)
|
Super Techie Join Date: Jun 2005 Posts: 274
| Re: small but odd! Quote: Originally posted by i_learn
[B]
int j=1,q;
q=j++ + ++j + j++;
printf("%d %d",q,j);
getch();
clrscr();
q=j++ + ++j + ++j;
printf("%d %d",q,j);
getch();
}
| it not that difficult if you understand how it works
6 4
16 7
would be the out put
let me explain
++j increment J by 1 before operation
J++ increment J by 1 after operation
for example the first one
j++ + ++j + j++
first thing done is (j++ + ++j)
first number is incremented after secound one before so you get
1+2=3
now that it done with the operation J is increase one for the J++ so now j =3
3+3=6 (j++ is incremented after operation)
and after that all done J increase to 4
sorry if I was confusing am trying to be clear. |
| |
11-30-2005, 07:19 PM
|
#7 (permalink)
|
Super Techie Join Date: Jun 2005 Posts: 274
| Quote: Originally posted by csamuels hmmm,
int j=1,q; // j=1 q=null
q=j++ + ++j + j++; // translates to q= 2 + 2 + 2 (2 cus of pre inc) and j now == 2 | This is wrong it translates too 1+2+3 |
| |
11-30-2005, 10:02 PM
|
#8 (permalink)
|
True Techie Join Date: Nov 2005 Posts: 115
| thanks all of you, but i got it last night, mgoldb2, the second one is not 16 and 7 but 18 and 7, check it out,
also, it is 2 +2+2, if yuo take it as 1+2+3 then with that thought process we end up wil still more odd results, unless of course i am seein it wrong, try your same idea with q=++j + ++j + j++;
where j is 1 and q is 0. |
| |
11-30-2005, 10:36 PM
|
#9 (permalink)
|
Super Techie Join Date: Jun 2005 Posts: 274
| Quote: Originally posted by i_learn
[B]thanks all of you, but i got it last night, mgoldb2, the second one is not 16 and 7 but 18 and 7, check it out,
| I just ran it the computer gave me 16 and 7 9 and 4 because both are done before calculation so you get 3+3 then the other is done afterward so you get 6+3 and the total is 9 and 4
In this case it is 3+3+3. that dont mean that it always work out having all the number the same.
You got to remember the computer think of it like this
q=((++j + ++j) + j++);
so each quwation in a parthies is done first and if it a J++ afterward it increases it but it does NOT wait till the end of all equtions.
acturally maybe if I showed it this way it would make more sence
q=++j + ++j + j++ is the same as
q=++j + ++j
q=q + j++ |
| |
12-01-2005, 01:26 AM
|
#10 (permalink)
|
True Techie Join Date: Nov 2005 Posts: 115
| how can it be?
i got 18 and 7 both ways, in theory and on the complier.......
let me see, j=1;
j++ + ++j + j++
will be q=6, j=4;(one pre inc, gives me j=2, and does that while on that expression, and 2 postincs makes j 4 after the expression is read)
then j++ + ++J + ++j;
similarly, 2 preincs, makes j 6, and that makes q 18, and one post inc makes j 7. my complier reutned 18 and 7 to me!!! i will check it again tho!
the order makes no difference, its the combination, that matters, u put a j++ + j++ + ++j or a j++ + ++j + j++ or a ++j + j++ + j++ the output will still be same, i dont understnad how brackets will help? |
| |  | | | Thread Tools | | | | Display Modes | Linear Mode |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | |