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

increments

If

int a=10, b;
b=++a + ++a;
printf("%d, %d, %d, %d\n", b, a++, a, ++a);

what is the result???
Dec 13 '09 #1
8 1910
when i tried it out it gave the result as
24 13 13 13
How did that happen?
Please explain!!!
Dec 13 '09 #2
The result is undefined. Try reading about sequence points. Search it on google.....
Dec 13 '09 #3
thank u
any other suggestions? :)
Dec 13 '09 #4
Banfa
9,065 Expert Mod 8TB
There is no need for other explanations or suggestions. It IS undefined behaviour because the C (or C++) standard says that it is and since the compiler is free to do absolutely anything once undefined behaviour is invoked any result could occur. For instance on my system using the exact same code I get the output

24, 13, 14, 14

The cause of the undefined behaviour in this case is that you have accessed a variable (a) more than once between sequence points where one or more or those accesses are for a write operation.

Wikipedia has good introductory articles on both Sequence Points and Undefined Behaviour.
Dec 13 '09 #5
hi,

i m not sure about the first part b=++a + ++a.
In this case when u say ++a, it just increments 'a' at its place by 1. a+b is evaluated by compiler in postfix manner.so,at the end both operator are having value 12.so,24.

but i m sure about printf part. In this case values are passed from left to right on to the stack.so ++a is evaluated first thats why 13,then a ,again 13,then a++ which will be evaluated after this statement so again 13 & b which is 24.
If u try to print a now,it will be 14.
Dec 14 '09 #6
Banfa
9,065 Expert Mod 8TB
zacksoniar, I am afraid you are wrong on the first count because the C/C++ standards specifically say that trying to access a variable more than once between sequence points were 1 or more of the access is to write (change the variable value) results in undefined behaviour.

Since the result of the expression is undefined behaviour the result you give could happen but so could any other result. The fact is that the value of such expressions changes from compiler to compiler and system to system, precisely because it is undefined behaviour. Such expressions should be avoided (and are generally unnecessary anyway).

Unfortunately you don't fair much better when explaining the printf behaviour. The order of evaluation of parameters in a function call is implementation defined. That is the implementation can do it any way it chooses but must document what the method is. Commonly parameters are evaluated either left to right or right to left. What you say may be true about the platform you are using but if you got into the habit of relying on that and then moved to a platform where it was different then you would suddenly find behaviour that you relied on didn't work as expected.

You should avoid implementation defined behaviour if at all possible and if it isn't possible it should be confined to a file by itself and clearly documented in case the software needs to be ported to another system.
Dec 14 '09 #7
hi,

I agree with u Banfa,but I tried to explain it with respect to what output whizgirl58 has specified.The compiler she is using,must be following right-to-left evaluation(++a part first).so its according to her compiler.I m still not sure about first part(b=++a + ++a).She must consider what u have said & then consider my opinion
Dec 15 '09 #8
I found explaination for printf("%d, %d, %d, %d\n", b, a++, a, ++a); part in
Kanitkar's book.There was similar kind of example.
Dec 15 '09 #9

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

Similar topics

10
by: anonymousnerd | last post by:
Apologies if this question seems stupid: How does one write a function that increments a value in Python? When I tried, the variable never changed. The session went like this: >>> def...
4
by: Nicolla MacPherson | last post by:
Hi I'm a newbie and want to display a pattern that will increment with each line of display. I thought i might be able to count the rows and use that info to increment my display. I've got my...
0
by: Nick | last post by:
Is it possible to have multiple auto increments in one column? Say I have two tables... 1. table 'messageboards' with fields ('id' , 'name') 2. table 'messagethreads' with fields ('id' ,...
5
by: someone | last post by:
Hi All, I need a function that only allows Increments by 10, 20, 30..... How do I achieve this in VB Script? Any help is appreciated. TIA,
12
by: Jim Fox | last post by:
I never noticed this myself. Had a user bring this up when then were updating a field that is a NUMBER, LONG INTEGER field. In successive rows, they entered into. 3 (typed in row 1) 4 (typed...
14
by: tlyczko | last post by:
I am looking to validate time worked in quarter-hour increments, e.g. ..25, .5, .75, 1.0, etc. etc. I know I can use a modulo statement for the evaluation but I could not find an explanation of...
3
by: forum | last post by:
Hi! what's the standard way for a "for" loop with float increments? Anton
7
by: Joe | last post by:
Is there a formula or algorhythm I can use to round up a series of numbers in increments of 100? 36 =100 89 =100 102 =200 112 =200 5 =100 225 =300
0
brokensword008
by: brokensword008 | last post by:
Hello everyone. I'm new here. I have a problem with Expression Web. I have more than a hundred pages created from my self-made template. My website sells products so I subscribed to RomanCart for...
1
by: phpuser123 | last post by:
I have a page that increments my session everytime,my passowrd and username are not correct..Buteven when I am reloading my page,the session is beinng incremented and as such I amgetting a defective...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
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
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
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...

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.