Solved

Increment & Decrement operators

Posted on 2000-04-10
26
298 Views
Last Modified: 2010-04-02
Why does Increment & Drecement operators behave differently on different operating systems.Why is it very unpredictable?
0
Comment
Question by:Ramprasad_Inala
  • 7
  • 3
  • 3
  • +9
26 Comments
 

Author Comment

by:Ramprasad_Inala
Comment Utility
Adjusted points from 50 to 75
0
 
LVL 10

Expert Comment

by:rbr
Comment Utility
What differences to you get?
I don't know any (if you take care of the overflow since an integer isn't the same size on different OSs)
0
 
LVL 10

Expert Comment

by:makerp
Comment Utility
things such as

i++ and ++i do not always resolve the same depending on platform/os

What differences to you get?
 

 
0
 
LVL 10

Expert Comment

by:rbr
Comment Utility
If i++ and ++i do not always resolve the same depending on platform/os you are doing something not defined or your compiler has a bug.
0
 
LVL 10

Expert Comment

by:rbr
Comment Utility
e.g.


i=1;


printf ("%d,%d,%d",i++,i++,i++);

would lead to different results for different compilers and options.
0
 
LVL 10

Expert Comment

by:makerp
Comment Utility
oh i just read it the other day in a book called .. wait for it

'industrial strength C++'

if thats wrong then well theres no hope for any one

:)
0
 
LVL 1

Expert Comment

by:guyss
Comment Utility
well, the problem occurs because you use
the ++ operator more than once in the same command, and ANSI does NOT define
what should happen first, these things are usually easily resolved by the programmer, simply write good code, and you won't have these problems.
0
 
LVL 12

Expert Comment

by:pjknibbs
Comment Utility
So long as you only ever use one on a line they should behave the same way between systems. When you start doing the multiple ++s, as in rbr's example, you start running into problems of which order your C compiler carries out the operations--this is not set out in the standard C specification, so compiler vendors are free to pick whatever suits them best. For the same reason you shouldn't really do things like:

if (c != NULL and *c == 400)

because not all compilers will evaluate the test for NULL before the second part of the if, and dereferencing a NULL pointer is almost always fatal.
0
 
LVL 10

Expert Comment

by:rbr
Comment Utility
To guyss: You are not correct. It is not the number of the ++ operaters since


printf ("%d,%d",i++,i); could lead to differnt reults too. The way how arguments are evaluated is not defined in C in general.

But I agree with your second statement.
0
 
LVL 1

Expert Comment

by:guyss
Comment Utility
you're right, of course...
wasn't thinking right...
my mistake.
0
 
LVL 10

Expert Comment

by:rbr
Comment Utility
To pjknibbs: Your are wrong too. Pls read
http://www.eskimo.com/~scs/C-faq/q3.8.html
0
 
LVL 12

Expert Comment

by:pjknibbs
Comment Utility
rbr: I don't see how the article you point out invalidates what I said--it merely said everything within two sequence points would be evaluated, *not* which order the sequence points would be evaluated in.
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
It does not say everything within two sequence points would be evaluated, it says that if an object is modified between sequence points, it can only be accessed in order to determine the value for that modification.
i.e. printf ("%d,%d",i++,i); is undefined, since i is modified by the i++, but the access in the second ,i is not for the purpose of determining that modified value.
This applies to all side effects, not just ++ and -- operators.
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Expert Comment

by:mathought
Comment Utility
I am afraid the unary operator ++ do have different implementation in different platforms. For example:

int *p;
p++;

Under some platforms (like DOS real mode) the size of a pointer is different than in others (like win32) thus ++ will have different effect.
0
 
LVL 1

Expert Comment

by:guyss
Comment Utility
not as far as the language is concerned,
as long as you're pointing to C structures, you're ok,
if you need to calculate addresses,
use sizeof directly, and not the ++ operator.
0
 
LVL 10

Expert Comment

by:rbr
Comment Utility
To Ramprasad_Inala: Pls give any feedback. We will not be able to help you.
0
 
LVL 9

Expert Comment

by:Pacman
Comment Utility
Ramprasad,

increment operator is same as "+= 1" which is same as " x = x + 1"

you say that
x = x + 1
behaves differently on different OS ?

I can't believe that.

0
 

Expert Comment

by:Namita
Comment Utility
There is no difference. They behave same on all OS.

It has a higher precedence always on all Operators but they are compiler dependent.





 

0
 
LVL 3

Expert Comment

by:sreenathk
Comment Utility
The statement itself is a ambiguous statement and should not be used in programing. You are correct that the behaviour is different on diff. os. Becauase some Os starts execution from the last parameter and some from the first.

Sreenath
0
 
LVL 7

Expert Comment

by:KangaRoo
Comment Utility
Despite the fact that the actual code with the problem is not given, the answer is given before, the order of evaluation with multiple increments between two sequence points is likely the problem, as rbr pointed out. There is nothing undefined about operator++ itself.

>> if (c != NULL and *c == 400)
To the best of my knowledge, first c!=NULL is evaluated anf *c==400 is only evalauted if c!=NULL. Guaranteed.
0
 
LVL 10

Accepted Solution

by:
rbr earned 75 total points
Comment Utility
Pls give some feedback !!!
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
>> if (c != NULL and *c == 400)
 depends on whether you've
#define and &
 or
#define and &&
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
I don't see any
#include <iso646.h>
in the above code
0
 
LVL 11

Expert Comment

by:alexo
Comment Utility
>> int *p;
>> p++;

The above code sequence modifies p to point to the next int.  It is the same as
  p = (int*)((char*)p + sizeof(int));

The "problem" is that sizeof(int) is implementation defined.


>> depends on whether you've [...]

Ozo!  Really!!!

  /* iso646.h standard header */
  #ifndef _ISO646
  #define _ISO646
  #define and            &&
  #define and_eq      &=
  #define bitand      &
  #define bitor            |
  #define compl      ~
  #define not            !
  #define not_eq      !=
  #define or            ||
  #define or_eq      |=
  #define xor            ^
  #define xor_eq      ^=
  #endif /* _ISO646 */
0
 
LVL 11

Expert Comment

by:alexo
Comment Utility
I don't see any int main() either.

If you were able to comment on my remark 17 minutes before it was posted, surely your amazing powers of prescience tell you that <iso646.h> will be #included sometime in the future.
0
 
LVL 7

Expert Comment

by:KangaRoo
Comment Utility
'and' what about section 2.5??
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now