Computer Science, asked by Harshi666, 1 year ago

explain me how "c++" in this sum become 3. explain clearly.

Attachments:

nitish8089: c++(if we seen ++ operator after the variable) it's post -increment operator
nitish8089: post-increment operator state that first we have to load the value of post-incremented(c++) variable in register or position the current value of c++(3 which is evaluted by ++c ) to the equation so c=3..
nitish8089: d=++c(3) + c++(3) post-increment not end ..
nitish8089: after loading the value c++(3) increase the value of post-increment variable by one(1) so c=c+1=3+1=4 this value go to memory location of c so now c=4 but this value does not affect our equation value... both are independent (not same)...
nitish8089: d=++c(3)+ c++(3) +4..
nitish8089: d=3+3+4=10
nitish8089: cout<
nitish8089: if we print d then output 10..
nitish8089: now question what is( c )will print if we will print if we print after ,d=++c +c++ +4
nitish8089: obviously c output 4 it will affect by both ++c or c++

Answers

Answered by Mahakali
2
Listen there is a great difference between + & ++ ; + is just simple addition in this case but c ++ mean that first add then the next time when c is used add again .if you dont understand the above theory look at the example given below eg. Let a be 2 , and b be 5 find c = a++b++a here a is used twice so ur second step will be = (2)+(1) + (5)+(1) + 3 due to the property mentioned above here the second a becomes 3 for the + mentioned earlier should be used . I hope my answer satiafies you dear

Harshi666: then... why is 3 written even in place of ++c in the question??
Harshi666: pls explain me Mahakali...
Mahakali: Mr if u see the question n think according to the computer then c is used twice so addition will be obvious here
Mahakali: N i request ypu to plz not u slang language with me
Harshi666: not clear
Harshi666: hey... r u telling me... do u really find a slang in my vocabulary??
Harshi666: k... what ever... try get clear of my doub2
Mahakali: Let y be 6 and x be 5 n let example be y++x++y here (6+ 1) + (5+1) + (5+1) here mark that when u add the operator ++ you add the variable by one and then when the variable is used the secong time again add it
Answered by Anonymous
2

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




Harshi666: oh...
Harshi666: check out the question... that I posted
Harshi666: and try to ans it
Harshi666: u will find difference...
Harshi666: k.. fn
Harshi666: thanks for putting ur effort for such lengthy ans
Similar questions