Solved

Increment & Decrement operators

Posted on 2000-04-10
26
303 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
ID: 2702805
Adjusted points from 50 to 75
0
 
LVL 10

Expert Comment

by:rbr
ID: 2703040
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
ID: 2703138
things such as

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

What differences to you get?
 

 
0
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
LVL 10

Expert Comment

by:rbr
ID: 2703155
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
ID: 2703161
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
ID: 2703162
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
ID: 2703244
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
ID: 2703256
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
ID: 2703258
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
ID: 2703264
you're right, of course...
wasn't thinking right...
my mistake.
0
 
LVL 10

Expert Comment

by:rbr
ID: 2703273
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
ID: 2703653
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
ID: 2703782
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
 

Expert Comment

by:mathought
ID: 2704813
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
ID: 2706561
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
ID: 2710712
To Ramprasad_Inala: Pls give any feedback. We will not be able to help you.
0
 
LVL 9

Expert Comment

by:Pacman
ID: 2711387
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
ID: 2711720
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
ID: 2712799
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
ID: 2719476
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
ID: 2721978
Pls give some feedback !!!
0
 
LVL 84

Expert Comment

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

Expert Comment

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

Expert Comment

by:alexo
ID: 2722357
>> 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
ID: 2722976
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
ID: 2724230
'and' what about section 2.5??
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
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.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

822 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