简体   繁体   中英

Can someone tell me how does this work ? (C programming)

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 ie j+10 = 25 then the new value of n is subtracted by 25 ie 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 ie 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.

  1. n++ will increment n and return the original result, so you've then got m = 16 ... .

  2. -j so you've got m = 16 - 15 ... .

  3. +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);

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

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.

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 .

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

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 .

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.

n的值在以m结尾的表达式中使用后, n递增。

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM