Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
752 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 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...