Search code examples
clanguage-lawyerterminology

What is the difference (if any) between "defined" and "well-defined"?


Recently I've seen this comment w.r.t. behavior (emphasis added):

Generally, it's not undefined, but not entirely well-defined either.

It confused me a bit, because if the behavior is not undefined, then it is defined. Hence, what is the difference (if any) between defined behavior and well-defined behavior?

Note that the standard has no term well-defined behavior. However, it has term well-defined semantics. So, the question can be similar: what is the difference (if any) between defined semantics and well-defined semantics?

In general: what is the difference (if any) between defined and well-defined?

Reason of the question: better understanding of the standard.


Solution

  • Well-defined is what we usually call code containing neither undefined, unspecified or implementation-defined behavior. The definition of a strictly conforming program from chapter 4 might be helpful:

    A strictly conforming program shall use only those features of the language and library specified in this International Standard. It shall not produce output dependent on any unspecified, undefined, or implementation-defined behavior, and shall not exceed any minimum implementation limit.

    Any snippet of C code fulfilling the above could informally said to be well-defined, as in defined by the ISO C language standard and portable. As opposed to for example implementation-defined meaning documented compiler-specific behavior.