Page 122 - C-Language
P. 122

Chapter 15: Constraints




        Remarks



        Constraints are a term used in all of the existing C specifications (recently ISO-IEC 9899-2011).
        They are one of the three parts of the language described in clause 6 of the standard (along side
        syntax and semantics).

        ISO-IEC 9899-2011 defines a constraint as a:


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

        (Please also note, in terms of the C standard, a "runtime-constraint" is not a kind of constraint and
        has extensively different rules.)


        In other words a constraint describes a rule of the language which would make an otherwise
        syntactically valid program illegal. In this respect constraints are somewhat like undefined
        behavior, any program which does not follow them is not defined in terms of the C language.

        Constraints on the other hand have a very significant difference from Undefined Behaviors.
        Namely an implementation is required to provide a diagnostic message during the translation
        phase (part of compilation) if a constraint is breached, this message may be a warning or may halt
        the compilation.


        Examples



        Duplicate variable names in the same scope


        An example of a constraint as expressed in the C standard is having two variables of the same
                                    1)
        name declared in a scope , for example:

         void foo(int bar)
         {
             int var;
             double var;
         }


        This code breaches the constraint and must produce a diagnostic message at compile time. This
        is very useful as compared to undefined behavior as the developer will be informed of the issue
        before the program is run, potentially doing anything.


        Constraints thus tend to be errors which are easily detectable at compile time such as this, issues
        which result in undefined behavior but would be difficult or impossible to detect at compile time are
        thus not constraints.






        https://riptutorial.com/                                                                               98
   117   118   119   120   121   122   123   124   125   126   127