small but odd!

Status
Not open for further replies.

i_learn

In Runtime
Messages
114
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 hell does it give me that output:(
 
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.
 
oh and the consider the 2 statements....as seperate ones, reset values of j and q! so sorry, for that confusion!
 
i_learn said:

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.
 
csamuels said:
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
 
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.
 
i_learn said:
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

q=++j + ++j + j++;

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++
 
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?
 
Status
Not open for further replies.
Back
Top Bottom