Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Post History

81%
+7 −0
Q&A Why can't we mix increment operators like i++ with other operators?

I'm experimenting with different operators and have a hard time understanding the outcome of certain expressions. I try to combine the ++ operators with other operators such as assignment in the sa...

1 answer  ·  posted 3y ago by Lundin‭  ·  last activity 3y ago by Lundin‭

#3: Post edited by user avatar Lundin‭ · 2020-10-06T11:24:40Z (over 3 years ago)
  • Why can't we mix increment operators with other operators?
  • Why can't we mix increment operators like i++ with other operators?
#2: Post edited by user avatar Lundin‭ · 2020-10-06T11:19:31Z (over 3 years ago)
  • I'm experimenting with different operators and have a hard time understanding the outcome of certain expressions. I try to combine the `++` operators with other operators such as assignment in the same expression. But I get mighty strange results when I use the same variable more than once. One example:
  • int i=0;
  • i=i++;
  • printf("%d\n",i);
  • This prints `0` on gcc and clang compilers, but `1` when I use icc, and so on.
  • clang tells me:
  • > warning: multiple unsequenced modifications to 'i' [-Wunsequenced]
  • `gcc -Wall` tells me:
  • > warning: operation on 'i' may be undefined [-Wsequence-point]
  • I don't understand these warnings, what's the meaning of "unsequenced" and "sequence points"?
  • ---
  • Various other attempts give similar strange and unpredicted results, with the behavior changing when I switch compiler. Some other examples that fail to behave deterministically:
  • - `i = i++ + ++i;`
  • - `i = array[i++];`
  • - `func(i, i++);`
  • - `*ptr++ = *ptr++;`
  • What is the reason behind all my problems with expressions like the ones above? I thought that operator precedence guaranteed a certain order of execution?
  • I'm experimenting with different operators and have a hard time understanding the outcome of certain expressions. I try to combine the `++` operators with other operators such as assignment in the same expression. But I get mighty strange results when I use the same variable more than once. One example:
  • int i=0;
  • i=i++;
  • printf("%d\n",i);
  • This prints `0` on gcc and clang compilers, but `1` when I use icc. And so on, different behavior with different compilers.
  • clang tells me:
  • > warning: multiple unsequenced modifications to 'i' [-Wunsequenced]
  • `gcc -Wall` tells me:
  • > warning: operation on 'i' may be undefined [-Wsequence-point]
  • I don't understand these warnings, what's the meaning of "unsequenced" and "sequence points"?
  • ---
  • Various other attempts give similar strange and unpredicted results, with the behavior changing when I switch compiler. Some other examples that fail to behave deterministically:
  • - `i = i++ + ++i;`
  • - `i = array[i++];`
  • - `func(i, i++);`
  • - `*ptr++ = *ptr++;`
  • What is the reason behind all my problems with expressions like the ones above? I thought that operator precedence guaranteed a certain order of execution?
#1: Initial revision by user avatar Lundin‭ · 2020-10-06T10:47:00Z (over 3 years ago)
Why can't we mix increment operators with other operators?
I'm experimenting with different operators and have a hard time understanding the outcome of certain expressions. I try to combine the `++` operators with other operators such as assignment in the same expression. But I get mighty strange results when I use the same variable more than once. One example:


    int i=0;
    i=i++;
    printf("%d\n",i);

This prints `0` on gcc and clang compilers, but `1` when I use icc, and so on. 

clang tells me:

> warning: multiple unsequenced modifications to 'i' [-Wunsequenced]

`gcc -Wall` tells me:

> warning: operation on 'i' may be undefined [-Wsequence-point]

I don't understand these warnings, what's the meaning of "unsequenced" and "sequence points"?

---

Various other attempts give similar strange and unpredicted results, with the behavior changing when I switch compiler. Some other examples that fail to behave deterministically:

- `i = i++ + ++i;` 
- `i = array[i++];`
- `func(i, i++);`
- `*ptr++ = *ptr++;`

What is the reason behind all my problems with expressions like the ones above? I thought that operator precedence guaranteed a certain order of execution?