473,461 Members | 2,008 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

regarding increment and decrement operator

question:
value of i is 10.
now give me answer of 1) i++ + i++ 2)i-- - i++ 3)++i - --i 4)--i + i--
Nov 30 '13 #1
5 1342
weaknessforcats
9,208 Expert Mod 8TB
This code will cause indeterminate behavior.

Variables are evaluated by the compiler before relationships. The compiler is free to evaluate the variables in any order. Therefore, different compilers will produce different results.

You cannot change a variable more than once in a statement.
Nov 30 '13 #2
dgunner71
110 100+
poojapatel -

What are you trying to accomplish with this code?
As Weaknessforcats noted, you will need to update for each purpose.


Dim i as Integer
i = 10

'Example Code here for this purpose
i = i + 5

'More Example Code here for this purpose
i = i + 10

'More Example Code here for this purpose
i = i - 5

Gunner
Nov 30 '13 #3
donbock
2,426 Expert 2GB
Your expression tries to change i more than once within the same statement. The C Standard says trying to modify the value of a variable more than once in the same statement is undefined behavior.

The C Standard does not impose any limitation on what happens when undefined behavior occurs.
  • You may get a build error.
  • You may get a run-time error.
  • You may get a result that makes some sort of sense.
  • You may get a result that makes no sense at all.
  • You may get a different result each time.
  • Your hard drive may be reformatted.
  • You may get a speeding ticket later that day.
  • Glaciers may advance.
  • Global air and ocean temperatures may rise enough to change the climate.
  • A comet may fly into the sun.
  • The earth may spiral into the sun.
  • Galaxies may collide.
It just isn't worth it. Avoid undefined behavior.
Dec 1 '13 #4
The simplest trick is to calculate the prefix operators first and then put the net value in the expression.
1. i++ + i++
No prefix operator. so, i=5
exp=5+5=10, i=5+1+1=7
2. i-- - i++
No prefix operator. so, i=5
exp=5-5=0, i=5-1+1=5
3. ++i - --i
net value of i= 5+1-1=5
exp=5-5=0, i=5+1-1=5
4. --i + i--
net value of i= 5-1=4
exp=4+4=8, i=5-1-1=3
Dec 13 '13 #5
donbock
2,426 Expert 2GB
@HSharma1729, what you say may seem plausible but I assure you there are compilers around that won't provide the results you expect. I wouldn't be surprised if there weren't compilers out there that give different answers depending on what level of optimization is selected.

Avoid undefined behavior.
Dec 13 '13 #6

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

Similar topics

1
by: Hendrix | last post by:
Hi, I would like to pick four elements from a multidemensional array and use the content of those elements. I want to do this without all the subscript calls required for individual access, just...
4
by: John | last post by:
I was wondering why the STL RB Tree implementation is used when the iterators of map are incremented/decremented to yield successors and predecessors? This takes O(log n) time (and a lot of memory...
8
by: lovecreatesbeauty | last post by:
Hello experts, Why can this difference between prefix increment/decrement and postfix increment/decrement reside in built-in operators for built-in data types? Thanks. // test.cpp // //...
5
by: Ian Pilcher | last post by:
I'm trying to figure out if an increment to a variable of an integer type, followed by a decrement, (or vice versa) is guaranteed to restore the variable to its initial value, even if the first...
2
by: rahul8143 | last post by:
hello, I am confused with following programs snippet code about how the answer comes? statement/expression is evaluated? 1) int i=5; i=i++*i++*i++*i++; why the answer is not 5*6*7*8= 1680 but...
8
by: Angel Tsankov | last post by:
Should pre/post increment/decrement return const or non-const? What about other functions?
5
by: Stuart | last post by:
Hi all, Iv'e got a page that has a mass amount of input fields, all of which require a decimal figure. To make it easier when it comes to inputting data, I'm trying to setup + and - links that...
17
by: Ashwin | last post by:
hi guys, i have overloaded the << operator.as shown below. ostream& operator<<(ostream &out, const student &a) { out<<a.idno; out<< " " ; // out<< a.name; out<< " " ; // out<< a.marks...
6
by: Kevin Walzer | last post by:
This code: #include <stdio.h> int main(void) { int n1, n2; //two integers n1 = 1; n2 = 1;
8
by: cpptutor2000 | last post by:
Could some C guru please help ? I have: int x = 20, y = 35; x = x++ + y++; y = ++x + ++y; printf("%d %d\n", x, y); The answers are 57, 94 I tried reasoning using the standard C semantics...
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
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.