Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Closed Thread
Old 11-30-2005, 02:47 PM   #1 (permalink)
 
True Techie

Join Date: Nov 2005

Posts: 115

i_learn

Default 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
i_learn is offline  
Old 11-30-2005, 04:06 PM   #2 (permalink)
office politics's Avatar
 
It's all just 1s and 0s

Join Date: Jan 2004

Location: in the lab

Posts: 4,410

office politics will become famous soon enough

Default

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.
office politics is offline  
Old 11-30-2005, 04:11 PM   #3 (permalink)
 
True Techie

Join Date: Nov 2005

Posts: 115

i_learn

Default

ok try
q= ++j + j++ + ++j;
i_learn is offline  
Old 11-30-2005, 04:18 PM   #4 (permalink)
 
True Techie

Join Date: Nov 2005

Posts: 115

i_learn

Default

the one to j++ + ++j + j++ is wrong, its not 7 and 15, but the first one is correct
i_learn is offline  
Old 11-30-2005, 04:29 PM   #5 (permalink)
 
True Techie

Join Date: Nov 2005

Posts: 115

i_learn

Default

oh and the consider the 2 statements....as seperate ones, reset values of j and q! so sorry, for that confusion!
i_learn is offline  
Old 11-30-2005, 07:18 PM   #6 (permalink)
 
Super Techie

Join Date: Jun 2005

Posts: 274

mgoldb2

Default 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.
mgoldb2 is offline  
Old 11-30-2005, 07:19 PM   #7 (permalink)
 
Super Techie

Join Date: Jun 2005

Posts: 274

mgoldb2

Default

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
mgoldb2 is offline  
Old 11-30-2005, 10:02 PM   #8 (permalink)
 
True Techie

Join Date: Nov 2005

Posts: 115

i_learn

Default

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 is offline  
Old 11-30-2005, 10:36 PM   #9 (permalink)
 
Super Techie

Join Date: Jun 2005

Posts: 274

mgoldb2

Default

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

Quote:
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++
mgoldb2 is offline  
Old 12-01-2005, 01:26 AM   #10 (permalink)
 
True Techie

Join Date: Nov 2005

Posts: 115

i_learn

Default

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?
i_learn is offline  
 
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On