int m, n, j; n=16; j=15; m = n++ -j+10; printf("%d", m);
Here, first, the old value of
n is given to
m and then it is incremented so the new value i get is 17 and then the expression is solved i.e.
j+10 = 25 then the new value of n is subtracted by 25 i.e 17-25. Am i right ? but the answer doesn't match the output
11. Then how does this work ? And also, i am new to programming and started learning C. Which book will you suggest is the best for me ? As I've no programming experience. Thank you.
m = n++ -j+10; is same as
m = n -j+10;
n = n + 1; // m is 11.
If it was
++n It would be
n = n + 1;
m = n -j+10; //m is 12.
then the expression is solved i.e. j+10 = 25
No. It would be
-j+10 = -5
My suggestion is, dont write complex expression unless you are completely sure what you are writing.
You've got a few things wrong there.
nand return the original result, so you've then got
m = 16 ....
-jso you've got
m = 16 - 15 ....
+10so you've got
m = 16 - 15 + 10.
Now the last time I did maths that would come out as
m = 11 like you're seeing.
If you wanted it to be
m = 17 - (15 + 10) then you wanted:
int m, n, j;
m = ++n -(j+10);
in fact the post increment operation is done on n after the operation... you have 16-15+10 = 11 but if you print n you should have 17.
to begin, you can read some book on basics but this example is not simple; it include the precedence of operator which can be tricky.
begin simple... it's quite simple to write unreadable code in c. http://www.cs.cf.ac.uk/Dave/C/node4.html
hope it helps
You're making two incorrect interpretations.
Firstly, as indicated in other answers,
n++ only increments
n after the entire expression has been evaluated.
Secondly, you have
-j+10. This is not equal to
-(j+10), so it is wrong to say that
25 and you are looking a
something - 25. Another way of view
n++ first returns the value of
n and then increments it.
so, the actual computation that takes place is
m = 16 - 15 + 10 which is 11
i think what you want is:
m = (n+1) - (j+10);
the use of the
++ operator is to increment the value of
n for future use after you use it's current value to compute
In answer to your question about a good book - you probably want to consider learning C++ instead of just plain old C, since C++ is a superset of C. And for C++ you need to get Bjarne Stroustrup's 'The C++ Programming Language'. It's easy enough to read and will last a long time on your bookshelf as a good reference.
m, first of all calculate
n - j + 10 and assign it to
m. After that
n++ is executed.
at the end
n = 17,
m = 11
In the expression
m = n++ -j+10;
The compiler treats the expression as
m= n++ ((-j)+10)
As the intialized values of n and j are
n = 16 and
j = 15. We have
m = 16++ ((-15)+10). We get output as
After the expression is executed
n will be incremented.
n is incremented after its value is used in the expression which ends up in
n++ is post incrementation. It only increments the value of
n after doing:
m = n++ -j+10;
++n is pre incrementation. It increments the value of
n before calculing m.
m = ++n -j+10;