The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
+1 vote
int main(void) 
  int p = 1; 
  int q = 0; 
  q = p++ + p++; 
  printf("%d %d",p,q); 
  return 0; 

Output is Showing $3,3$ How ?

asked in Programming by Junior (505 points)
edited by | 112 views

2 Answers

+1 vote
Look at it this way
q=q + p++;

so the first time when p is encountered in this expression is treated as 1, the next ++ increments p by 1 so p becomes two. the second operand p is now treated as 2 so (1+2) becomes three. Now after that ++ is encountered which makes p=3. So this is why p becomes three and q becomes three
answered by (27 points)
The above code you have written is not the same as gi en in question. You can't write p++ in another line since p value will be used in the question is old one(1) nd the p value used in your code for computing q is new one(2). So, I think answer should be 3,2
0 votes
Step first- p=1 stored and then incremented

Step two- p=2 stored and then incremented to 3

Then q=1+2=3

And p has been incremented and and its value is final value stored in p is 3
answered by Active (1.1k points)

Related questions

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true

46,785 questions
51,234 answers
66,584 users