473,395 Members | 1,999 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

c++ unary increment operator

Hi,
I'm confused with C++'s unary increment operator(++).
Consider the following code:

//////////////////////////////////////////////////////////////////////
#include <iostream>
using namespace std;

int main()
{
int a=2;
a= (a==2 ? a+=100:0) + (a>3 ? a+=200:1000) + a++;
cout << a << endl;
return 0;
}
//////////////////////////////////////////////////////////////////////

I have run this code both on linux (using g++) and windows (VS.NET).
Surprisingly, the resaults were incompatible!
g++ answers: 507
VS answers: 707

Can anyone help?
Jul 19 '05 #1
1 4308

"MSDousti" <MS******@myrealbox.com> wrote in message
news:bf*************************@posting.google.co m...
Hi,
I'm confused with C++'s unary increment operator(++).
Consider the following code:

//////////////////////////////////////////////////////////////////////
#include <iostream>
using namespace std;

int main()
{
int a=2;
a= (a==2 ? a+=100:0) + (a>3 ? a+=200:1000) + a++;
cout << a << endl;
return 0;
}
//////////////////////////////////////////////////////////////////////

I have run this code both on linux (using g++) and windows (VS.NET).
Surprisingly, the resaults were incompatible!
g++ answers: 507
VS answers: 707
The language does not specify the order in which the
expressions:

(a==2 ? a+=100:0)
(a>3 ? a+=200:1000)
a++

.... are evaluated.
Can anyone help?


Break the code down to where you have sufficient control
over evaluation order. Look up 'sequence points'. I can't
tell you what you need here, since you don't say which result
you want.

-Mike
Jul 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Carlos Ribeiro | last post by:
I was checking the Prolog recipe in the Cookbook: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/303057 It's a clever implementation that explores some aspects of Python that I wasn't...
2
by: Dave Theese | last post by:
Am I correct in saying that it is not possible to overload unary + and unary -?
5
by: Ruben Campos | last post by:
Some questions about this code: template <typename T> class MyTemplate; template <typename T> MyTemplate <T> operator- (const MyTemplate <T> & object); template <typename T> MyTemplate <T>...
17
by: Anoob | last post by:
Can we consider () unary operator when calling a function, in exps eq. f(), ( 1 + 2). But when we call function f( 10 ) it is a binary operator. Even if we pass f( 10, 20) as we are using ,...
2
by: shan | last post by:
Hi to everybody, I am begginer in C programming language. My simple doubt is the difference between postfix & prefix unary operator plus. (i.e) i++ and ++i . plz give me an example program...
2
by: Javier Estrada | last post by:
1. For types smaller than int, when I compile: class MyClass { static void Main(string args) { x = 10; y = -x; }
4
by: Alistair Welchman | last post by:
I have a Hashtable of ints keyed on Guids, and I want to do the following: foreach ( DataRow row in dsWorkDays.Tables.Rows ) { Guid PersonId = (Guid)row; DateTime Day = (DateTime)row;
28
by: dspfun | last post by:
I'm trying to get a good understanding of how unary operators work and have some questions about the following test snippets. int *p; ~!&*++p--; It doesn't compile, why? The problem seems to be...
16
by: JoseMariaSola | last post by:
How may operators and operands does (typename) expression has? I'd say one operator, the cast operator, and two operands: typename and expression. But everywhere I read, cast is categorized as...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.