(++ , --)operators

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

34 Replies - 3372 Views - Last Post: 26 October 2017 - 12:36 PM Rate Topic: -----

#1 hahaie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 25-October 17

(++ , --)operators

Posted 25 October 2017 - 10:44 PM

hi all,
i have a question.
In your opinion why output of following code is 24?
int a=10,c=0;
c=++a + ++a;
cout<<c;


And on the same basis why output of following code is 37?
int a=10,c=0;
c=++a + ++a + ++a;
cout<<c;


you can for run above code in https://www.onlinegd...ne_c++_compiler
thanks

Is This A Good Question/Topic? 0
  • +

Replies To: (++ , --)operators

#2 ndc85430  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 588
  • View blog
  • Posts: 2,479
  • Joined: 13-June 14

Re: (++ , --)operators

Posted 25 October 2017 - 10:46 PM

Why do you think the values are what they are? Have you tried working it out?
Was This Post Helpful? 1
  • +
  • -

#3 hahaie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 25-October 17

Re: (++ , --)operators

Posted 25 October 2017 - 11:08 PM

yes i run in https://www.onlinegd...ne_c++_compiler

This post has been edited by ndc85430: 26 October 2017 - 12:01 AM
Reason for edit:: Removed quote of previous post.

Was This Post Helpful? 0
  • +
  • -

#4 ndc85430  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 588
  • View blog
  • Posts: 2,479
  • Joined: 13-June 14

Re: (++ , --)operators

Posted 25 October 2017 - 11:58 PM

So, tell us what you found out and tell us what your understanding of the pre and post increment operators is.

Also, there's no need to quote the previous post. Please just hit "Reply" in future.

This post has been edited by ndc85430: 26 October 2017 - 12:00 AM

Was This Post Helpful? 0
  • +
  • -

#5 hahaie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 25-October 17

Re: (++ , --)operators

Posted 26 October 2017 - 12:28 AM

ok! :sweatdrop:
value of a is 10.++a => a=11 and again a++=> a=12 therefor 11+12=23 no 24!

Forgive me,i not found edit button for above post :sweatdrop: :sweatdrop:
edited:
value of a is 10.++a => a=11 and again ++a=> a=12 therefor 11+12=23 no
Was This Post Helpful? 0
  • +
  • -

#6 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3714
  • View blog
  • Posts: 13,468
  • Joined: 08-August 08

Re: (++ , --)operators

Posted 26 October 2017 - 05:27 AM

See this link:
http://en.cppreferen...ator_precedence
Now which operations in your code will take place first?
Was This Post Helpful? 0
  • +
  • -

#7 hahaie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 25-October 17

Re: (++ , --)operators

Posted 26 October 2017 - 06:05 AM

it is obvious that ++a is higher priority of +.
for getting smarter result you can use this code:
int a=10,c=0;
c=(++a) + (++a);
cout<<c;


an output is 24.why?
Was This Post Helpful? 0
  • +
  • -

#8 GazinAtCode  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 69
  • Joined: 26-September 16

Re: (++ , --)operators

Posted 26 October 2017 - 06:44 AM

Because a is incremented twice before the addition, so you're effectively adding 12 to 12.

In the second case, a apparently gets incremented twice before the first addition takes place (12 + 12 = 24), and then it is incremented once again (13) before the second addition (24 + 13 = 37).

This post has been edited by GazinAtCode: 26 October 2017 - 06:46 AM

Was This Post Helpful? 0
  • +
  • -

#9 hahaie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 25-October 17

Re: (++ , --)operators

Posted 26 October 2017 - 07:14 AM

I think your analysis is not correct.
please see this:
Posted Image
Was This Post Helpful? 0
  • +
  • -

#10 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3714
  • View blog
  • Posts: 13,468
  • Joined: 08-August 08

Re: (++ , --)operators

Posted 26 October 2017 - 07:25 AM

That's not what I would expect, or what I get.
	int a=10, a1 = 10,c=0, b;
	b = ++a1 + ++a1;
	c= ++a + ++a + ++a;
	printf("b = %d  c = %d\n",b,c);


My results:
b = 23  c = 36


Since the operations are done from right to left:
b = 12 + 11 = 23

c = 13 + 12 + 11 = 36
Was This Post Helpful? 0
  • +
  • -

#11 jon.kiparsky  Icon User is online

  • Chinga la migra
  • member icon


Reputation: 10682
  • View blog
  • Posts: 18,296
  • Joined: 19-March 11

Re: (++ , --)operators

Posted 26 October 2017 - 07:27 AM

View Posthahaie, on 26 October 2017 - 09:14 AM, said:

I think your analysis is not correct.


His analysis is agreeing with the actual output, and yours isn't, so I'm inclined to think his analysis has one big advantage over yours.

You might want to add a little nuance to your conception of how ++a is actually evaluated. The expression has a side effect and a value. Your assumption is that the both of these things happen "at the same time" - that the compiler finds ++a and increments a and then leaves behind the value of the expression. That's a reasonable way to think about things, but it's not working for you here. Can you come up with another hypothesis, which would agree with the observed phenomena?
Was This Post Helpful? 1
  • +
  • -

#12 GazinAtCode  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 69
  • Joined: 26-September 16

Re: (++ , --)operators

Posted 26 October 2017 - 07:48 AM

I was basically trying to explain the results you were getting.

GCC produces 37, while MSVC gives 39.

For a better understanding of what is going on behind the scenes, you can view the corresponding disassembly code:

GCC:
0x401364	addl   $0x1,-0xc(%ebp)
0x401368	addl   $0x1,-0xc(%ebp)
0x40136c	mov    -0xc(%ebp),%eax
0x40136f	lea    (%eax,%eax,1),%edx
0x401372	addl   $0x1,-0xc(%ebp)
0x401376	mov    -0xc(%ebp),%eax
0x401379	add    %edx,%eax
0x40137b	mov    %eax,-0x10(%ebp)
0x40137e	mov    -0x10(%ebp),%eax
0x401381	mov    %eax,(%esp)
0x401384	mov    $0x489940,%ecx



MSVC:
00DF3C3C  mov         eax,dword ptr [a]  
00DF3C3F  add         eax,1  
00DF3C42  mov         dword ptr [a],eax  
00DF3C45  mov         ecx,dword ptr [a]  
00DF3C48  add         ecx,1  
00DF3C4B  mov         dword ptr [a],ecx  
00DF3C4E  mov         edx,dword ptr [a]  
00DF3C51  add         edx,1  
00DF3C54  mov         dword ptr [a],edx  
00DF3C57  mov         eax,dword ptr [a]  
00DF3C5A  add         eax,dword ptr [a]  
00DF3C5D  add         eax,dword ptr [a]  
00DF3C60  mov         dword ptr [c],eax


This post has been edited by GazinAtCode: 26 October 2017 - 07:49 AM

Was This Post Helpful? 1
  • +
  • -

#13 hahaie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 25-October 17

Re: (++ , --)operators

Posted 26 October 2017 - 07:56 AM

View PostCTphpnwb, on 26 October 2017 - 07:25 AM, said:

That's not what I would expect, or what I get.
	int a=10, a1 = 10,c=0, b;
	b = ++a1 + ++a1;
	c= ++a + ++a + ++a;
	printf("b = %d  c = %d\n",b,c);


My results:
b = 23  c = 36


Since the operations are done from right to left:
b = 12 + 11 = 23

c = 13 + 12 + 11 = 36

thanks but result of your code in https://www.onlinegdb.com/ is b=24 , c=37
why?
Was This Post Helpful? 0
  • +
  • -

#14 jimblumberg  Icon User is online

  • member icon

Reputation: 5334
  • View blog
  • Posts: 16,602
  • Joined: 25-December 09

Re: (++ , --)operators

Posted 26 October 2017 - 07:58 AM

This problem is not about operator precedence, or evaluation order but about Undefined behavior involving modifying a variable multiple times without a sequence point.

Quote

GCC produces 37, while MSVC gives 39.

And both compilers are correct, isn't undefined behavior fun?

Jim
Was This Post Helpful? 3
  • +
  • -

#15 hahaie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 25-October 17

Re: (++ , --)operators

Posted 26 October 2017 - 08:04 AM

View PostGazinAtCode, on 26 October 2017 - 07:48 AM, said:

I was basically trying to explain the results you were getting.

GCC produces 37, while MSVC gives 39.

For a better understanding of what is going on behind the scenes, you can view the corresponding disassembly code:

GCC:
0x401364	addl   $0x1,-0xc(%ebp)
0x401368	addl   $0x1,-0xc(%ebp)
0x40136c	mov    -0xc(%ebp),%eax
0x40136f	lea    (%eax,%eax,1),%edx
0x401372	addl   $0x1,-0xc(%ebp)
0x401376	mov    -0xc(%ebp),%eax
0x401379	add    %edx,%eax
0x40137b	mov    %eax,-0x10(%ebp)
0x40137e	mov    -0x10(%ebp),%eax
0x401381	mov    %eax,(%esp)
0x401384	mov    $0x489940,%ecx



MSVC:
00DF3C3C  mov         eax,dword ptr [a]  
00DF3C3F  add         eax,1  
00DF3C42  mov         dword ptr [a],eax  
00DF3C45  mov         ecx,dword ptr [a]  
00DF3C48  add         ecx,1  
00DF3C4B  mov         dword ptr [a],ecx  
00DF3C4E  mov         edx,dword ptr [a]  
00DF3C51  add         edx,1  
00DF3C54  mov         dword ptr [a],edx  
00DF3C57  mov         eax,dword ptr [a]  
00DF3C5A  add         eax,dword ptr [a]  
00DF3C5D  add         eax,dword ptr [a]  
00DF3C60  mov         dword ptr [c],eax


thanks but it is tested by me in other applications(with result deferences).I things Now,it is better other application not say.

My friends.Now i thinks,other application not say,only c++ or c. :nono:
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3