Home > Article > Backend Development > What\'s the Difference Between C \'s \'Ill-Formed; No Diagnostic Required\' and Undefined Behavior?
Undoubtedly, the C standard introduces an extensive array of definitions for behaviors categorized as "unclear." However, the phrase "the program is ill-formed; no diagnostic required" has raised questions regarding its distinction from other behavioral categories.
Defining Well-Formed and Ill-Formed Behaviors:
The C standard classifies programs as either well-formed or ill-formed. Well-formed programs adhere to both the syntax and diagnosable semantic rules. Conversely, ill-formed programs violate these rules and should ideally not compile.
Implementation-Defined, Unspecified, and Undefined Behaviors:
Implementation-defined and unspecified behaviors are distinct from well-formed behaviors, but they do not imply erroneous code. In the former case, the implementation must document its specific actions, while in the latter, no documentation is necessary. Undefined behavior, on the other hand, implies that the program is erroneous, according to 1.3.13.
Compilers, Undefined Behavior, and Ill-Formed Programs:
Despite the term "erroneous" suggesting that compilers should terminate compilation with an error message, the C standard allows for silent handling of undefined behavior. Notably, compilers often do not issue warnings or terminate compilation due to undefined behavior.
Ill-Formed Programs and Diagnostic Requirements:
The use of "no diagnostic required" alongside "ill-formed" presents a paradox. If ill-formed programs should not compile, then allowing compilers to ignore them silently would be counterintuitive. However, the standard explicitly allows for this possibility.
Conclusion:
The distinction between "ill-formed; no diagnostic required" and "undefined behavior" remains hazy. While there may be an association between ill-formed programs and incorrect code, and undefined behavior with run-time issues, this is not always consistent. Ultimately, the "no diagnostic required" aspect is paramount, as it overrides the expectation of diagnostic generation in the case of ill-formed programs.
The above is the detailed content of What\'s the Difference Between C \'s \'Ill-Formed; No Diagnostic Required\' and Undefined Behavior?. For more information, please follow other related articles on the PHP Chinese website!