473,396 Members | 2,102 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.

output of the code cout<<a+ a++ + --a; comes out to be 16 when a=5 what is the logic?

int a=5;
cout<< a+ a++ + --a ;
output is
16
logic behind the output??
Jul 14 '10 #1
9 5221
@umangbansal
somebody reply please..
Jul 14 '10 #2
code green
1,726 Expert 1GB
I'm never comfortable with ugly looking code but it looks fairly simple to me
Expand|Select|Wrap|Line Numbers
  1. a=5
  2. cout 5 ... 5
  3. a++
  4. a=6
  5. cout 5 + 6 ... 11
  6. a--
  7. a=5
  8. cout 5 + 6 + 5... 16
Jul 14 '10 #3
weaknessforcats
9,208 Expert Mod 8TB
This code:

Expand|Select|Wrap|Line Numbers
  1. cout<< a+ a++ + --a ;
has indeterminate results.

The compiler is free to evaluate variables in any order within a sequence point (a statement). Therefore, if a variable is changed more than once within a statement you cannot be sure of the result.
Jul 14 '10 #4
@weaknessforcats
but this code always gives the same result..
Jul 14 '10 #5
weaknessforcats
9,208 Expert Mod 8TB
Are you using the same compiler?

Visual C++ 2008 gives 12 as a result.

As I said, the results are indeterminate.
Jul 14 '10 #6
Banfa
9,065 Expert Mod 8TB
but this code always gives the same result..
Accessing a variable more than once between sequence points where one of those accesses changes the variables value is undefined behaviour (as defined by the standard).

A program exhibiting undefined behaviour may do anything including but not limited to
  • Exit without explanation
  • Produce some sort of exception
  • Appear to operate correctly
  • Operate correctly the whole time you are testing it and only go wrong the first time it is critically important that it works
  • Format your hard drive
  • Make demons fly out of your nose

The compiler is under no constraints with the code it produces once undefined behaviour is invoked.

Trying to determine the output of a program exhibiting undefined behaviour is pointless because it is under no constraint to stick to whatever assumptions you have made while making that determination.

Your only real solution is to not write code that exhibits undefined behaviour.


I am beginning to feel like a scratched record on the subject a quick forum search would have thrown up several recent threads on this very subject.
Jul 14 '10 #7
donbock
2,426 Expert 2GB
banfa: "I am beginning to feel like a scratched record on the subject ..."

You mean a broken record, as in one where the stylus skips backwards and repeats the same audio over and over again.

It is true that a scratch might produce a backwards skip. However, in today's lingo, scratch refers to the effect made when a record is rotated back and forth through a small angle.

You're must be too young to remember vinyl records, but too old to be familiar with rap. ;-)
Jul 14 '10 #8
Dheeraj Joshi
1,123 Expert 1GB
Make demons fly out of your nose
Again demons started to fly. lol.

Regards
Dheeraj Joshi
Jul 15 '10 #9
Banfa
9,065 Expert Mod 8TB
You're must be too young to remember vinyl records, but too old to be familiar with rap. ;-)
I think that would be a very narrow time frame. I do remember and in fact did own vinyl I just got my metaphors mixed :D
Jul 15 '10 #10

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

Similar topics

3
by: Albert Spencil | last post by:
It once was necessary to type "view-source" before the URL in Location to view the javascript original coding for Netscape. The default was the output code of the script. Now the default is the...
10
by: jeff regoord | last post by:
A user inputs a float value. The scanf() function gets the value. However, I need to create an error handler with an if else statement saying invalid input if the input is not a number. Does...
3
by: David P. Donahue | last post by:
I'm trying to come up with the best (fastest, as this code will be run often) way to accomplish the following: I have a dynamic string array X (as one grabbed from a GetFiles() or...
8
by: Martin Eyles | last post by:
Hi, I have an asp.net page, which uses the response.write method calls in the Page_Load method to output dynamically to the page. Unfortunately, this always inserts text at the beginning of the...
0
by: Ryan Ternier | last post by:
To give some background on the solution, it is made up of 3 websites, and 9 projects. We just moved this over from .NET 1.1 and have it working fine now. The only issue I have is when I am...
13
by: Kantha | last post by:
Hi all, I have declared an Union as follows typedef union { struct interrupt_bits { unsigned char c_int_hs_fs_status : 1, c_setup_intflag : 1,
0
by: Mads Westen | last post by:
Hi, I'm trying to code a application that can create a new email in Outlook I have coded my project in VS 2003, but now I have upgraded to VS 2005. I can not build my project anymore, I get a...
0
by: hasankacd | last post by:
hi, Im using vb.net programme with db2. when I insert a record the following error comes even update the record. But I can retrieve the data with out any problem. But the particular query can...
3
by: cie | last post by:
Hi, anyone please help me when I try to close a connection to the database from a static method, "Unreachable code detected" error occured. What's the problem actually? Thank you before
3
by: easygeeza98 | last post by:
Hi Folks Iam a newbee to the forum and am having a few problems with Access. Have any of you seen this before in Access 2003. I have two subforms based on separate queries. I use a mixture of...
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
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...

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.