473,396 Members | 1,760 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,396 software developers and data experts.

Pre-increment in C++

Why would the following statement assign 12 to x not 11?
Expand|Select|Wrap|Line Numbers
  1.     int i=4;
  2.     int x = (++i) + (++i);
  3.     cout << x; // prints 12
  4.  
Jan 12 '11 #1

✓ answered by horace1

when ++ and -- are used in an expression such as
Expand|Select|Wrap|Line Numbers
  1. (i * j++) + (i * j)
will the value of j in (i * j) be that before or after the increment in (i * j++)?
In addition to evaluating the expression, the value of the variable in memory is altered (this is called a side effect). Do not use a variable more than once in an expression if one (or more) of the references has one of these operators attached to it. The standard does not specify the order in which the operands of an operator are evaluated and there is no guarantee when an affected variable will change its value. For example:
Expand|Select|Wrap|Line Numbers
  1.  int i=2, j=3;
  2.  printf("%d \n", (i * j++) + (i * j));
  3.  
on a Windows PC the gcc compiler prints 12 the Boralnd Turobo C V3 compiler prints 14.
for we look at your example
Expand|Select|Wrap|Line Numbers
  1.     int i=4;    
  2.     int x = (++i) + (++i);
  3.     cout << x; // prints 12
  4.  
the gcc compiler prints 12 the Boralnd Turobo C V3 compiler prints 11.

3 2074
Rabbit
12,516 Expert Mod 8TB
Everything within the parentheses are evaluated first. So i gets incremented twice before they're used to calculate the sum.
Jan 12 '11 #2
horace1
1,510 Expert 1GB
when ++ and -- are used in an expression such as
Expand|Select|Wrap|Line Numbers
  1. (i * j++) + (i * j)
will the value of j in (i * j) be that before or after the increment in (i * j++)?
In addition to evaluating the expression, the value of the variable in memory is altered (this is called a side effect). Do not use a variable more than once in an expression if one (or more) of the references has one of these operators attached to it. The standard does not specify the order in which the operands of an operator are evaluated and there is no guarantee when an affected variable will change its value. For example:
Expand|Select|Wrap|Line Numbers
  1.  int i=2, j=3;
  2.  printf("%d \n", (i * j++) + (i * j));
  3.  
on a Windows PC the gcc compiler prints 12 the Boralnd Turobo C V3 compiler prints 14.
for we look at your example
Expand|Select|Wrap|Line Numbers
  1.     int i=4;    
  2.     int x = (++i) + (++i);
  3.     cout << x; // prints 12
  4.  
the gcc compiler prints 12 the Boralnd Turobo C V3 compiler prints 11.
Jan 12 '11 #3
Banfa
9,065 Expert Mod 8TB
Actually that code can assign any value it wants to x because it exhibits undefined behaviour.

Under undefined behaviour the program (and the compiler) are free to do absolutely anything it wants, no constraints are placed on it. Results could be anything from appearing to work correctly to producing an unexpected result to making demons fly out of your nose.

What is more because the behaviour is undefined it does not even have to do the same thing when run another time.

The reason this is undefined behaviour is that the code accesses a variable (i) is access between sequence points more than once where at least one of those accesses is to write its value.

You should avoid undefined behaviour at all costs.
Jan 12 '11 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

21
by: Headless | last post by:
I've marked up song lyrics with the <pre> tag because it seems the most appropriate type of markup for the type of data. This results in inefficient use of horizontal space due to UA's default...
7
by: Alan Illeman | last post by:
How do I set several different properties for PRE in a CSS stylesheet, rather than resorting to this: <BODY> <PRE STYLE="font-family:monospace; font-size:0.95em; width:40%; border:red 2px...
2
by: Buck Turgidson | last post by:
I want to have a css with 2 PRE styles, one bold with large font, and another non-bold and smaller font. I am new to CSS (and not exactly an expert in HTML, for that matter). Is there a way to...
3
by: Michael Shell | last post by:
Greetings, Consider the XHTML document attached at the end of this post. When viewed under Firefox 1.0.5 on Linux, highlighting and pasting (into a text editor) the <pre> tag listing will...
8
by: Jarno Suni not | last post by:
It seems to be invalid in HTML 4.01, but valid in XHTML 1.0. Why is there the difference? Can that pose a problem when such a XHTML document is served as text/html?
7
by: Rocky Moore | last post by:
I have a web site called HintsAndTips.com. On this site people post tips using a very simply webform with a multi line TextBox for inputing the tip text. This text is encode to HTML so that no...
9
by: Eric Lindsay | last post by:
I can't figure how to best display little snippets of shell script using <pre>. I just got around to organising to bulk validate some of my web pages, and one of the problems occurs with Bash...
23
by: Xah Lee | last post by:
The Concepts and Confusions of Pre-fix, In-fix, Post-fix and Fully Functional Notations Xah Lee, 2006-03-15 Let me summarize: The LISP notation, is a functional notation, and is not a...
14
by: Schraalhans Keukenmeester | last post by:
I am building a default sheet for my linux-related pages. Since many linux users still rely on/prefer viewing textmode and unstyled content I try to stick to the correct html tags to pertain good...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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.