Reputation: 121
int m, n, j;
n=16;
j=15;
m = n++ -j+10;
printf("%d", m);
Output: 11.
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.
Upvotes: 1
Views: 293
Reputation: 397
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 11
.
After the expression is executed n
will be incremented.
Upvotes: 0
Reputation: 57690
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.
Upvotes: 4
Reputation: 1942
for 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
Upvotes: 0
Reputation: 309
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;
Upvotes: -1
Reputation: 61
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
Upvotes: 1
Reputation: 131
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.
Upvotes: 0
Reputation: 8147
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 m
.
Upvotes: 0
Reputation: 17376
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 j+10
is 25
and you are looking a something - 25
. Another way of view -j+10
is 10-j
.
Upvotes: 0
Reputation: 34912
You've got a few things wrong there.
n++
will increment n
and return the original result, so you've then got m = 16 ...
.
-j
so you've got m = 16 - 15 ...
.
+10
so 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;
n=16;
j=15;
m = ++n -(j+10);
printf("%d", m);
Upvotes: 1
Reputation: 60721
n
is incremented after its value is used in the expression which ends up in m
.
Upvotes: -1