# 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 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.

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

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.

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.

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.

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

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 is incremented after its value is used in the expression which ends up in m.

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;

Category:c# Views:0 Time:2012-01-25
Tags: increment

## Related post

• What is unit testing? 2008-08-04

I saw many questions asking 'how' to unit test in a specific language, but no question asking 'what', 'why', and 'when'. What is it? What does it do for me? Why should I use it? When should I use it (also when not)? What are some common pitfalls and

• Evidence for sealed class performance benefit 2008-08-05

I have come across a lot of optimization tips which say that you should mark your classes as sealed to get extra performance benefits. I ran some tests to check the performance differential and found none. Am I doing something wrong? Am I missing the

• Using a DOS command in Ruby script 2008-08-05

How do I call shell commands from inside of a Ruby program? How do I then get output from these commands back into Ruby? --------------Solutions------------- This explanation is based on this commented Ruby script from a friend of mine. If you want t

• How to calculate Big O notation from piece of code 2008-08-06

Most people with a degree in CS will certainly know what Big O stands for. It helps us to measure how (in)efficient an algorithm really is and if you know in what category the problem you are trying to solve lays in you can figure out if it is still

• DISCUSSION - Uploading and Storing Mass Images 2008-08-06

So I'm using an app that stores images heavily in the DB. What's your outlook on this? I'm more of a type to store the location in the filesystem, than store it directly in the DB. What do you think are the pros/cons? --------------Solutions---------

• storing files to a database or just to the local hard-disk? 2008-08-06

So I'm using an app that stores images heavily in the DB. What's your outlook on this? I'm more of a type to store the location in the filesystem, than store it directly in the DB. What do you think are the pros/cons? --------------Solutions---------

• Where is a good place to start programming GUIs for windows? 2008-08-07

I have experience writing console and network client/server applications in C and C++, but I know next to nothing about using the win32 visual API, MFC, Qt, wxWidgets, etc. Where is a good place to start, and what method should I specialize in, so as

• How to include PHP files that require an absolute path? 2008-08-07

I have a directory structure like the following; /script.php /inc/include1.php /inc/include2.php /objects/object1.php /objects/object2.php /soap/soap.php Now, I use those objects in both script.php and /soap/soap.php, I could move them, but I want th

• How do I retrieve my MySQL username and password? 2008-08-07

I lost my MySQL username and password. How do I retrieve it? --------------Solutions------------- Stop the MySQL process. Start the MySQL process with the --skip-grant-tables option. Start the MySQL console client with the -u root option. List all th

• Best Multi-Language Documentation Generator 2008-08-07

What is the best documentation generator? I want to something that will easily add templates for documenting functions, classes, etc. I know there are several tools out there -- from Visual Studio plugins to external applications that take code files

• Where can I get the Windows Workflow "wca.exe" application? 2008-08-07

I am walking through the MS Press Windows Workflow Step-by-Step book and in chapter 8 it mentions a tool with the filename "wca.exe". This is supposed to be able to generate workflow communication helper classes based on an interface you provide it.

• Using Xming X Window Server over a VPN 2008-08-07

I have the Xming X Window Server installed on a laptop running Windows XP to connect to some UNIX development servers. It works fine when I connect directly to the company network in the office. However, it does not work when I connect to the network

• CSharpCodeProvider Compilation Performance 2008-08-07

Is CompileAssemblyFromDom faster than CompileAssemblyFromSource? It should be as it presumably bypasses the compiler front-end. --------------Solutions------------- CompileAssemblyFromDom compiles to a .cs file which is then run through the normal C#

• How can I execute dynamic SQL in c#? 2008-08-07

I can do an eval("something()"); to execute the code dynamically in JavaScript. Is there a way for me to do the same thing in C#? What I am exactly trying to do is that I have an integer variable (say i) and I have multiple properties by the names: "

• C# eval code from string 2008-08-07

I can do an eval("something()"); to execute the code dynamically in JavaScript. Is there a way for me to do the same thing in C#? What I am exactly trying to do is that I have an integer variable (say i) and I have multiple properties by the names: "

• DVCS Choices - What's good for Windows? 2008-08-07

So I want to get a project on a distributed version control system, such as mercurial, git, or bazaar. The catch is that I need the Windows support to be good, i.e. no instructions that start off with "install cygwin...". Now I've heard that git's Wi

• Automating VMWare or VirtualPC 2008-08-07

I'm currently experimenting with build script, and since I have an ASP.net Web Part under source control, my build script should do that at the end: Grab the "naked" Windows 2003 IIS VMWare or Virtual PC Image from the Network Boot it up Copy the Fil

• Why should I learn Lisp? 2008-08-07

I really feel that I should learn Lisp and there are plenty of good resources out there to help me do it. I'm not put off by the complicated syntax, but where in "traditional commercial programming" would I find places it would make sense to use it i

• Learning Regular Expressions 2008-08-07

I don't really understand regular expressions. Can you explain them to me in an easy-to-follow manner? If there are any online tools or books, could you also link to them? --------------Solutions------------- The most important part is the concepts.

• Using ConfigurationManager to load config from an arbitrary location 2008-08-07

I'm developing a data access component that will be used in a website that contains a mix of classic ASP and ASP.NET pages, and need a good way to manage its configuration settings. I'd like to use a custom ConfigurationSection, and for the ASP.NET p