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
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...
#3: Post edited
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
- 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
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?