28

C standards talk about constraints, e. g. ISO/IEC 9899:201x defines the term

constraint
restriction, either syntactic or semantic, by which the exposition of language elements is to be interpreted

and says in chapter Conformance

If a ‘‘shall’’ or ‘‘shall not’’ requirement that appears outside of a constraint or runtime-constraint is violated, the behavior is undefined.

In chapter Environment, Subsection Diagnostics it is said

A conforming implementation shall produce at least one diagnostic message (identified in an implementation-defined manner) if a preprocessing translation unit or translation unit contains a violation of any syntax rule or constraint, even if the behavior is also explicitly specified as undefined or implementation-defined.

So, it is important to know what are the constraints in C, for example for compiler writers to judge when diagnostics are required, or for C programmers when diagnostics rather than just undefined behaviour can be expected.
Now, there are sections all over the standard document with the title Constraints, but I cannot find definitive wording as to what exactly the term constraint covers in the standard.

  • Are the constraints everything that appears in the sections titled Constraints?
  • Is every requirement that is stated outside of those sections not a constraint?
  • Is there a comprehensive description of constraint in the standard that I missed?
1
  • 4
    Interesting question, and funny wording in the standard. Taken literally it sounds as if violating a "shall [not]" inside a constraint is not necessarily UB, but I think they mean it's always UB with the additional requirement that a "constraint" violation requires a mandatory diagnostic by the implementation. Oct 29, 2015 at 12:01

7 Answers 7

11

Are the constraints everything that appears in the sections titled Constraints?

In the sense of n1570 3.8 (a restriction imposed on programs which requires a conforming implementation to issue a compile-time diagnostic message when violated), I think yes.

Is every requirement that is stated outside of those sections not a constraint?

In the sense of 3.8, I think yes, but for a more circular reason: The standard's structure is fairly formal. Whenever applicable there seems to be an explicit Constraints section. Therefore I understand that by definition anything which is not in a Constraints section is not a constraint in the sense of 3.8.
There are a few "shall" clauses outside Constraints sections which appear completely compile-time enforceable, cf. below for a few examples. They are often in adjacent Semantics sections. I may be missing subtleties which prevent compile-time detection in the general case (so that a diagnosis cannot be made mandatory), or perhaps the standard is not completely consistent. But I would think that a compiler could simply translate a violating program, exactly because the requirements are not in a Constraints section.

Is there a comprehensive description of constraint in the standard that I missed?

I think 3.8 is all you get. I try to explore the term below and agree that the definition is unsatisfying.


I looked deeper into the standard to find that out. Here is my research.

The term constraint

Let's start with the basics. The definition of "constraint" in 3.8 which you quote is surprisingly hard to understand, at least without context ("restriction, either syntactic or semantic, by which the exposition of language elements is to be interpreted"). "Restriction" and "constraint" are synonyms, so that the rewording doesn't add much; and what is meant by "exposition of language elements"?? Exposition is a word with several meanings; let's take "writing or speech primarily intended to convey information" from Dictionary.com, and let's assume they mean the standard with that. Then it means basically that a constraint in this standard is a constraint of what is said in this standard. Wow, I wouldn't have guessed that.

Constraints as per 3.8

Pragmatically just examining the actual Constraints sections in the standard shows that they list compile time restrictions imposed on conforming programs. This makes sense because only compile-time constraints can be checked at compile time. These additional restrictions are those which cannot be expressed in the C syntax.1

Constraints outside Constraints sections

Most uses of "shall" outside of Constraints sections impose restrictions on a conforming implementation. Example: "All objects with static storage duration shall be initialized (set to their initial values) before program startup", a job of a conforming implementation.

There are a few "shall" clauses imposing restrictions on a program (not the implementation) outside of Constraints sections though. I would argue that most fall in the same category as the "runtime constraints [...] on a program when calling a library function" mentioned in 3.18. They seem to be run time constraints which are not generally detectable at compile time (so that diagnostics can not be mandatory).

Here are a few examples.

In 6.5/7 n1570 details the much-debated aliasing rules:

An object shall have its stored value accessed only by an lvalue expression that has one of the following types:

  • a type compatible with the effective type of the object
  • a qualified version of a type compatible with the effective type of the object, [...]

In 6.5.16.1, "Simple Assignment":

If the value being stored in an object is read from another object that overlaps in any way the storage of the first object, then the overlap shall be exact[..]."

Other examples concern pointer arithmetic (6.5.6/8).

Shall clauses which could be in Constraints sections

But then there are other shall clauses whose violation should be detectable at compile time; I would not have blinked if they had appeared in the respective Constraints section.

  • 6.6/6, "Cast operators in an integer constant expression shall only convert arithmetic types to integer types" (under "Semantics"); what can you detect at compile time if you cannot detect types of constants and casts?
  • 6.7/7, "If an identifier for an object is declared with no linkage, the type for the object shall be complete by the end of its declarator" (under "Semantics"). To me is seems to be a basic compiler task to detect whether a type is complete at some point in the code. But of course, I have never written a C compiler.

There are a few more examples. But as I said, I would think that an implementation is not required to diagnose violations. A violating program which manages to sneak past the compiler simply exposes undefined behavior.


1 For example, I understand that the syntax doesn't deal with types -- it only has generic "expressions". Therefore every operator has a Constraints section detailing the permissible types of its arguments. Example for shift operators: "Each of the operands shall have integer type." A program which is trying to shift the bits of a float is violating this constraint, and the implementation must issue a diagnostic.

9
  • 1
    Thanks for your elaborate answer; it sounds conclusive. Would you (dis)approve the following two details? 1. Since the requirement in 6.3.2.2 "The (nonexistent) value of a void expression (an expression that has type void) shall not be used in any way, and implicit or explicit conversions (except to void) shall not be applied to such an expression" appears outside of a constraint section, assignment of the value of a void expression is no constraint violation, right? (So no diagnostic is required, though every implementation of any reasonable quality would produce one.)
    – Armali
    Nov 2, 2015 at 8:20
  • 2. Consider 7.7 Characteristics of floating types <float.h> §2 The macros, their meanings, and the constraints (or restrictions) on their values are listed in 5.2.4.2.2. Keith Thompson put it in comp.std.c: I think 7.7 is using the word "constraints" in its ordinary English sense, not in the sense defined by the standard. (It probably shouldn't.)
    – Armali
    Nov 2, 2015 at 8:30
  • 1
    @Armali I would think that in 1. indeed, a diagnostic is not mandatory (but that, indeed, I'd also be astonished if there wasn't one). I'm not sure whether one can construe situations where the compiler can't see it, e.g. function declarations with a conflicting return type in different translation units (in the calling TU a void function is declared returning int). Not sure whether that qualifies. 2. I think Keith Thompson is right, indicated also by the wording "(or restrictions)" in the standard. Nov 2, 2015 at 9:55
  • 1
    That said, I don't feel totally confident about all this, and I wouldn't rely too much on the standard being completely coherent.-- But with respect to the 7.7 and 5.2.4.2.2. float.h again: These are cleary requirements/restrictions imposed on a conforming implementation and have nothing to do with "Constraints" in the sense of 3.8 which impose restrictions on conforming programs. (Namely those restrictions which are not expressible in the grammar, like what types can arguments of operators have -- I understand that the C syntax description doesn't deal with types). Nov 2, 2015 at 10:37
  • 1
    Constraints only impose restrictions on Strictly Conforming C Programs.
    – supercat
    Jul 23, 2021 at 19:16
7

The C committee addressed this issue in the response to Defect Report # 033. The question in that defect report was:

Is a conforming implementation required to diagnose all violations of ''shall'' and ''shall not'' statements in the standard, even if those statements occur outside of a section labeled Constraints?

The author of that defect report suggested a couple of possible alternative ways of interpreting the language of the standard. The second alternative he listed said (in part):

Syntax rules are those items listed in the Syntax sections of the standard. Constraints are those items listed in the Constraints sections of the standard.

Part of the committee's response was:

Suggested Interpretation #2 is the correct one.

I believe that covers your questions fairly completely, but just to state answers to your questions more directly:

  • Are the constraints everything that appears in the sections titled Constraints?
  • Is every requirement that is stated outside of those sections not a constraint?

A "constraint" is a requirement that is stated in a section explicitly marked "Constraints". Any requirement stated outside such a section is not a constraint.

  • Is there a comprehensive description of constraint in the standard that I missed?

At least as far as I know, the standard itself doesn't contain a more specific statement about what is or isn't a constraint, but the linked defect report does.

2

Are the constraints everything that appears in the sections titled Constraints?

It appears they are mostly (there are some cases which are not, fx: it's stated that "Incrementing is equivalent to adding 1" in one of the constraint sections).

Is every requirement that is stated outside of those sections not a constraint?

I haven't seen a "constraint" outside those sections.

Is there a comprehensive description of constraint in the standard that I missed?

Probably not, if there were an authoritative such it would be in the standard and probably be the "constraint" sections (and explicitly mentioned that these are all "constraints").

My interpretation is that the chapter 3 should be interpreted so that every use of the defined terms would have the meaning defined in that section. Especially everywhere the term "constraint" is used it should be understood according to your first quote.

Your second quote is no exception. It's noted in the definition of the term "constraint" that there is no requirement that the constraint is explicitely termed a constraint. This means that you have to determine if it's a "constraint" by checking if it's a such restriction.

However there seem to be quite a few examples of "shall" and "shall not" that could be taken to be such restrictions without explicitely termed as such. That would leave all the occurrences "shall" and "shall not" be mandating or prohibiting a certain behavior of the implementation - and if these are not fulfilled, then yes the behavior may be undefined (since you're using an implementation that doesn't conform to the standard).

It looks like all that fits the definition of "constraint" seem to occur under a "constraint" section, and everything in the "constraint" sections seem to be "constraints".

1
  • Thanks for your answer. You say I haven't seen a "constraint" outside those sections. But consider for example 7.7 Characteristics of floating types <float.h> &sect;2 The macros, their meanings, and the constraints (or restrictions) on their values are listed in 5.2.4.2.2. That suggests that 5.2.4.2.2 contains constraints, while there is no mention of the term constraint there. My problem is whether and how I can recognize a constraint if I see one that's not named so.
    – Armali
    Oct 30, 2015 at 7:57
1

Are the constraints everything that appears in the sections titled Constraints?

Yes. Every syntactic and semantic restrictions mentioned in the standard are constraints.

For example, a constraint on Constant expressions (C11-6.6/3):

Constant expressions shall not contain assignment, increment, decrement, function-call, or comma operators, except when they are contained within a subexpression that is not evaluated.115)

Therefore, the constant expressions

3 = 5;
10++;

shows constraint violation.

Note that in this case shall requirement as well as constraint both are violated.

Is every requirement that is stated outside of those sections not a constraint?

For standard conforming C, yes. A shall requirement on integer constant expression (C11-6.6/6):

An integer constant expression117) shall have integer type [...]

For example, an integer constant expression is required for size of a non-variable length array. Therefore,

int arr[5+1.5];

violates the shall requirement. The type of expression 5+1.5 is not integer type. This shall requirement is out of constraint.

It should be noted that a shall requirement may be a constraint too.

17
  • 1
    Perhaps because the last sentence doesn't make much sense. The question is about the standard, not about any (possibly non-conforming) implementation or even program. [Otherwise I think you are correct that all constraints in the sense of 3.8. in n1570 are inside sections explicitly labeled "Constraints".] Oct 29, 2015 at 10:51
  • @PeterSchneider; what? Your comment doesn't make much sense. For non-confirming answer of last question could be no.
    – haccks
    Oct 29, 2015 at 10:55
  • Thanks for your answer (I didn't downvote it). That would mean for example that assignment of the value of a void expression would be no constraint violation, right?
    – Armali
    Oct 29, 2015 at 10:57
  • 1
    The question of the OP is "Is every requirement [appearing in the book named "ISO C standard"] outside of those sections [labeled "Constraints"] not a constraint?" In other words, is the contents of the sections in the standard which are labled "Constraints" all and only all constraints in the standard. Your answer "For standard conforming C, yes" is non-applicable. We are not talking about C, we are talking about a specific book. The answer can only be "yes" or "no", or perhaps "I do not know". (Oh, I didn't downvote either, just tried to be helpful.) Oct 29, 2015 at 11:03
  • @PeterSchneider; And that specific book is standard confirming, i.e. I am talking about ISO C11 standard.
    – haccks
    Oct 29, 2015 at 11:06
1

In my work in requirements engineering, the words "constraint" and "requirement" have different scope. It is important, also for the standard, to define those explicitly. I searched the word "constraint" in the standard and it seems I may draw the following conclusion:

A constraint is a limitation of either the input (pre-condition) or the output (post-condition) of the behavior the section of the standard describes. For input it means the input must adhere to the constraint (e.g. argc shall be positive). For output it means it must satisfy the constraint for any following unit of the standard to have a well-defined input (its pre-condition).

A requirement is part of the specification of the behavior of the section of the standard. "Shall" is a positive description of what is required; "shall not" is generally a limitiation, but not a constraint - it may participate though in meeting a constraint on its output.

Constraints and requirements can be seen as "external interfaces" (the constraints) and "system behavior/processing" (the requirements).

Shall generally denotes a requirement (a phrase without "shall" is hence not a requirement). "Shall" used in a constraint is then either used to define the input or output (e.g. argc shall be positive) or specifies behavior concerning validating the constraint (e.g. "...shall give a diagnostic message").

Strictly speaking, "shall" used in specifying behavior of validating an input constraint should not be listed in the constraint section (should not be listed in the interface specification) but in a processing section (behavior section).

Note that there can be no validation of an output constraint as the output should adhere to the specification; only a next uit can check those constraints if they are in its input constraints.

This may be a personal view but it seems to fit the uses of these words in the standard.

3
  • Thanks for your answer. This seems a coherent view, but I'm afraid it doesn't quite fit the standard. For example, you say For input it means it can assume the constraint is met … but may check for the constraint (it doesn't have to), while the standard says A conforming implementation shall produce at least one diagnostic message … if a preprocessing translation unit or translation unit contains a violation of any syntax rule or constraint (so it has to check).
    – Armali
    Oct 30, 2015 at 15:17
  • Also it talks about a ‘‘shall’’ or ‘‘shall not’’ requirement that appears outside of a constraint, which suggests to me that a requirement can well appear inside a constraint, and that sounds like a meaning of the term constraint different from what you described.
    – Armali
    Oct 30, 2015 at 15:17
  • Note: "argc shall be positive" is a questionable example "The value of argc shall be nonnegative." C11 5.1.2.2.1 2 - it could be 0. Oct 5, 2016 at 17:03
0

constraint

restriction, either syntactic or semantic, by which the exposition of language elements is to be interpreted

This means that every explicit restriction for program logic or syntax set by the c standart in any way is a constraint. This includes syntactic constraints (e.g. Blocks must be terminated with a ;) and semantic constraints (e.g. You shall not use a variable before initializing it), basicly everything that is either syntacticly (notation-wise) or semanticly (usage of correct notation-wise) not allowed or defined as not allowed (undefined behaviour).

Is every requirement that is stated outside of those sections not a constraint?

I do think that all explicit requirements for the programming in the C language fall either under a syntactic or semantic constraint.

Is there a comprehensive description of constraint in the standard that I missed?

Not to my knowledge.

4
  • 1
    Just because a constraint is a restriction, does that make every restriction a constraint?
    – Columbo
    Oct 29, 2015 at 9:15
  • @Columbo I do think every explicit restriction is a constraint. Do you have counter examples?
    – Magisch
    Oct 29, 2015 at 9:15
  • Thanks for your answer (I didn't downvote it). It seems a legitimate point of view. However, there is also an opposite view, e. g. All constraints are explicitly defined in the standard, in paragraphs clearly marked "Constraints".
    – Armali
    Oct 29, 2015 at 10:43
  • @Armali As language lawyering often is, this is important only in fringe cases.
    – Magisch
    Oct 29, 2015 at 10:44
0

The purpose of constraints in the Standard is to specify conditions where a conforming implementation would be required to issue a diagnostic, or to allow implementations could process a program in ways contrary to what would be required absent the constraint in cases where doing so might be might be useful than the otherwise-specified behavior. Although Strictly Conforming C Programs are not allowed to violate constraints (no program that violates a constraint is a Strictly Conforming C Program), no such restriction applies to programs that are intended to be Conforming but not Strictly Conforming.

The C Standard was written as a compromise among multiple overlapping factions, including

  1. those who thought that it should discourage programmers from writing code that wouldn't work on all platforms interchangeably
  2. those who thought it should allow programmers who were targeting known platforms to exploit features that were common to all of the platforms they'd need to support, even if they wouldn't be supportable on all platforms
  3. those who thought that compilers should be allowed to diagnose constructs and actions which would be performed more often by accident than deliberate intent
  4. those who thought that it should allow programmers to do things like perform address calculations which would appear erroneous, but which would, if performed precisely as specified, yield the address of the object the programmer was expecting.

In order to achieve a consensus among these groups, the Standard imposed limits on what could be done within Strictly Conforming C Programs, but also write the definition of Conforming C Program broadly enough that almost no useful programs would be branded non-conforming no matter how obscure the extensions upon which they rely. If a source-code construct would violate a diagnosable constraint, but an implementation's customers would find it useful anyhow, then the implementation could output a diagnostic which its customers could ignore (even an unconditional: "Warning: This implementation doesn't bother outputting diagnostics its author thinks are silly, other than this one" would suffice) and everybody could get on with life.

6
  • But "A strictly conforming program shall use only those features of the language and library specified in this International Standard" is itself outside of constraint. It seems the compiler could treat every program as a strictly conforming program and since it violates this shall, say this program has undefined behavior, then do anything the compiler likes. Feb 26, 2022 at 8:23
  • @TuffContender: A conforming but useless compiler could say "If this program matches one specific useless program that nominally exercises the translation limits, behave like that program, and otherwise bomb the stack in such a way as to achieve results indistinguishable from that program", which under the as-if rule would allow it to simply behave like the useless program without even looking at the source code. From a strictly normative standpoint, the standard really impose any requirements on anything other than strictly conforming programs.
    – supercat
    Feb 26, 2022 at 16:43
  • Is this a problem of wording in the C standard? In the C++ draft, "shall" means "must", and the range of "behavior" is limited in programs. I can't imagine what undefined behavior of a implementation is, as specified in the C standard. E.g., "Each enumerated type ... shall be capable of representing the values of all the members of the enumeration", it's so weird of the standard to allow the implementation to violate this "shall" and see it as undefined behavior. I think the standard shall focus on conforming implementations and mention conforming-or-not programs. Feb 27, 2022 at 3:15
  • @TuffContender: I don't think there was ever a consensus as to whether the Standard was supposed to specify a full useful language, or a set of core language features which implementations intended for different platforms and purposes would extend as appropriate for those platforms and purposes. Nor, from what I can tell, was there any consensus as to what aspects of the Standard were supposed to be normative and what parts were merely guidelines. Unless or until the authors of a Standard can reach consensus on such issues, a useful and meaningful normative standard will be impossible.
    – supercat
    Feb 27, 2022 at 19:27
  • @TuffContender: What would be most useful in a C Standard would be for many parts of it to replace most requirements for implementations with strong recommendations, but add a requirement that implementations which deviate from recommended practice document any such deviations.
    – supercat
    Feb 27, 2022 at 19:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.