Distinguishing Undefined Behavior from Ill-Formedness
Undefined behavior (UB), ill-formedness, and implementation-defined behavior are distinct concepts in C . However, the standard's terminology can be confusing, particularly with the phrase "ill-formed; no diagnostic required."
Ill-Formed Programs and Diagnosability
An ill-formed program violates the syntax or semantic rules of the language. According to the standard, it "is otherwise only defined as being not well-formed." Logically, this implies that an ill-formed program should not compile.
However, the standard allows for implementations to have extensions that are ill-formed according to the standard but do not alter the behavior of well-formed programs. In these cases, the implementation must diagnose the use of the extension but is permitted to continue execution.
Undefined Behavior and Diagnosability
Undefined behavior refers to program behavior that cannot be predicted by the standard. Implementations are free to define the behavior in any way, including by suspending execution or silently ignoring the problem. Unlike ill-formedness, UB does not necessarily imply that the program should not compile.
"Ill-Formed; No Diagnostic Required"
The term "ill-formed; no diagnostic required" can be interpreted as referring to situations where a program violates the syntax or semantic rules, but the implementation is not required to diagnose the error. This would essentially make it equivalent to UB, except that implementations may still silently exit or continue execution.
The reason for using such wording is likely to allow for flexibility in implementation. It enables implementations to handle errors that would otherwise be ill-formed in a manner that may be useful in practice.
Conclusion
Although the C standard's wording is sometimes inconsistent, the general rule is that erroneous or ill-formed code requires a diagnostic unless the standard explicitly states otherwise or it is undefined behavior. In the case of "ill-formed; no diagnostic required," it is essentially synonymous with undefined behavior, giving implementers some latitude in how they handle certain errors.
The above is the detailed content of What\'s the Difference Between Undefined Behavior and Ill-Formedness in C ?. For more information, please follow other related articles on the PHP Chinese website!