#include <stdio.h>
main() {
int pro;
int dot;
int tot;
char prelude[] = "\nNow we shall do some simple mathematics\n\n";
printf("%s", prelude);
pro = 3;
dot = 5;
tot = pro + dot;
printf("%d", tot);
dot += 23;
printf("\n%d\n", tot);
return 0;
}

- 52,653
- 6
- 59
- 97

- 3
- 3
-
6what do you mean? – Sourav Ghosh Jan 12 '16 at 20:01
-
1Perhaps dot += 23; should be tot += 23; – bruceg Jan 12 '16 at 20:02
-
1@bruceg No, I think the OP wants `tot` to change by changing `dot`. – Iharob Al Asimi Jan 12 '16 at 20:02
-
May I ask, what makes you think that modifying `dot` should alter `tot`? – Iharob Al Asimi Jan 12 '16 at 20:03
-
1@iharob Ah. In that case, maybe he should get rid of the original assignment tot = pro + dot, and create a macro up at the top: #define TOT (pro + dot), or something. – bruceg Jan 12 '16 at 20:09
1 Answers
Because C does not offer automatic data binding. When you use the assignment operator (or Addition assignment in your case), you are assigning the VALUE of the statement on the right hand side of the expression.
variable = statement
means that statement
will be calculated to some value, and this value will be assigned to variable
according to variable
's data type.
This means that you are assigning a value, not an actual statement to that variable.
To explain it better:
tot = pro + dot;
does the following:
- I need to assign something that I must calculate to the variable
tot
. - Something is a statement,
pro + dot
. - This statement is an addition, an operation that takes two operands.
- Operand 1 is
pro
, it evaluates to 3. (means that C will replace pro with 3 in that statement) - Operand 2 is
dot
, it evaluates to 5. - All of the operands are evaluated.
- The statement is:
3 + 5
; - This evaluates to 8, an integer.
- The expression has become
tot = 8;
- Assign the value 8 to
tot
. This means, go to the memory address of the variable represented bytot
, and write the integer 8 (in accordance with the C standard/machine architecture).
If you do
dot += 23;
C understands this:
dot += 23; // can be translated to `dot = dot + 23;'
Like before:
dot
means 8, 23 means 23.- statement: 8 + 23
- The statement evaluates to 31.
dot = 31;
, meaning write the integer 31 to the memory ofdot
.
Now tot
, is unaffected by that. Why? Because tot
is a space in your memory, that holds the value 8. It doesn't know that 8 was created by adding 2 other variables. It's just 8. So changing the other variables, will not affect this one.
If you do dot += 23;
, you are changing the memory of the variable dot
, not that of tot
.
What you were expecting, is called data binding, and is a much higher level feature than what C offers.

- 3,411
- 4
- 26
- 42
-
Many thanks K. Gkinis. So if I want to alter the 'tot' int, i would have had to repeat the 'tot = pro + dot' after the compound assignment right? I have tried it like that beforehand to see if it works, of course it did, yet I had the feeling I missed something since repetition was so unnatural. Many thanks again, it is now clear :) – Mihai Ardelean Jan 12 '16 at 21:19
-
Try to name your variables with as descriptive names as possible like exampleInteger1 or test_variable (and stick to one [coding style](https://en.wikipedia.org/wiki/Programming_style)) – RaidenF Jan 12 '16 at 21:27
-
Oh and I would recommend reading the excellent [Teach yourself C](http://www.amazon.com/Teach-Yourself-C-Herbert-Schildt/dp/0078823110) or any of the other [recommended readings](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list), since you are a beginner and want to learn correctly (as stated in a previous post of yours). Following one of those books, will teach you everything you need and more, and believe me, especially the 1st one, doesn't take much time (you don't even have to read a large part at the end). – RaidenF Jan 12 '16 at 21:27
-