explain me how "c++" in this sum become 3. explain clearly.
Answers
Hi,
d = ++c + c++ + 4
To understand what is happening in above line of code first of all you have to learn what is statement.
So what is statement?
Line of code which does something is called statement.
What we are doing in that line/statement?
We are assigning calculated result of "++c + c++ + 4" to "d" variable.
What is the link of "++c" and "c++" with statement?
First of all look at this line carefully
d = (++c) + c++ + 4
i put parenthesis around "++c" because this will get calculated before whole statement
notice that i haven't put any parenthesis around "c++" because...
"++c" will be calculated before execution of statement and "c++" will be calculated
after execution of statement.
So now you know the difference of "++c" and "c++" (i hope so)
_____________________________________________________________________________________
let's understand it by looking into the value of "c" variable in program
=> given: c = 2
so "c" variable is containing the 2, c variable is pointing to a memory location which contains number 2
during execution of program below line will be executed
d = ++c + c++ + 4
so you already know that "++c" will be calculated before execution of statement
so now that statement will look like below
d = 3 + 3++ + 4
and remember now c variable is pointing to memory location which contains number 3
on next step 3 + (3++) + 4 will be calculated to number 10 but program will still remember that he has
to calculate the value of "3++"
so the variable "d" will be equals to 10, but after execution of statement c variable will be 4