Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
776 views
in Technique[技术] by (71.8m points)

printf - Encountered a weird warning : operation on 'C' may be undefined [-Wsequence-point]

#include <stdio.h>
int main(void)
{
    unsigned char A = 0xEB;
    unsigned char B = 0xE5;
    unsigned char C = 0x0; 
    printf("%#x
",A^B);
    
    printf("%d %#x",C = A^B,C = A^B);//This line cause the warning

    //*[Warning] operation on 'C' may be undefined [-Wsequence-point]*
}

My compiler: TDM-GCC 9.2.0

What caused this warning?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
6.5 Expressions
...
2    If a side effect on a scalar object is unsequenced relative to either a different side effect on the same scalar object or a value computation using the value of the same scalar object, the behavior is undefined. If there are multiple allowable orderings of the subexpressions of an expression, the behavior is undefined if such an unsequenced side effect occurs in any of the orderings.84)
84) This paragraph renders undefined statement expressions such as i = ++i + 1; a[i++] = i; while allowing i = i + 1; a[i] = i;
C 2011 Online Draft

In the statement

printf("%d %#x",C = A^B,C = A^B);

the expressions C = A^B have a side effect (assign a new value to C) and they are unsequenced relative to each other (function arguments are not guaranteed to be evaluated from left to right, right to left, or any other order, and their evaluations may even be interleaved), hence the warning. Even though C is being assigned the same value both times, the behavior is still undefined.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...